10 实例讲解微信公众号开发-图书信息保存

图书信息保存

现在我们已经能够通过调用微信公众号的扫码藏书功能获取到我们的藏书信息,但是这些信息仅仅在微信公众号上进行了显示,没有保存下来,为了将我们藏书保存后续查看,还需要将这些图书信息记录到数据库中。

在之前安装的xampp中包含了mysql数据库,只需要在xampp-control开启mysql服务就可以使用。

10 实例讲解微信公众号开发-图书信息保存_第1张图片
mysql-config.png

打开后在浏览器中输入 http://127.0.0.1/phpmyadmin 就可以进入mysql的控制界面。

通过左侧的新建按钮新建一个数据库wxbook.

10 实例讲解微信公众号开发-图书信息保存_第2张图片
createtable.png

在数据库wxbook中增加一个表 wb_bookinfo,表结构可以使用phpMyAdmin图形化方式创建,也可以使用SQL语句创建。

10 实例讲解微信公众号开发-图书信息保存_第3张图片
wx_bookinfo.png

建表SQL语句

CREATE TABLE `wxbook`.`wb_bookinfo` (
`isbn10` VARCHAR(20)NULL DEFAULT NULL ,
`isbn13` VARCHAR(20) NOT NULL ,
`title`VARCHAR(200) NULL DEFAULT NULL ,
`author`VARCHAR(100) NULL DEFAULT NULL ,
`price` FLOAT(4) NULL DEFAULT NULL ,
`image`VARCHAR(256) NULL DEFAULT NULL ,
`translator` VARCHAR(100) NULL DEFAULT NULL,
`publisher` VARCHAR(100) NULL DEFAULT NULL ,
`summary` VARCHAR(1024) NULLDEFAULT NULL ,
`rating` DECIMAL(4) NULL DEFAULT NULL ,
`createtime` DATETIMENOT NULL DEFAULT CURRENT_TIMESTAMP )
ENGINE = InnoDB COMMENT = 'book information’;

在处理scancode_waitmsg的消息中,增加入库动作,代码如下:

case "scancode_waitmsg":
$isbn = substr($object->ScanCodeInfo->ScanResult, 7);
$content = "isbn:" . $isbn;
$douBanObj = new DoubanApi();
$bookInfo = $douBanObj->getDoubanInfo($isbn);
$image = $bookInfo["images"]["large"];
if (is_array($bookInfo['author']) && sizeof($bookInfo['author']) > 0) {
$bookInfo['author'] = $bookInfo['author'][0];
} else {
$bookInfo['author'] = '';
}
$content = array();
$summary = mb_substr($bookInfo['summary'], 0, 200, 'utf-8') . '...';
$content[] = array(
"Title" => "已成功添加:" . $bookInfo["title"],
"Description" => $summary,
"PicUrl" => $image,
"Url" => "",
);
/*将图书信息入库处理*/
$db= new Wb_db();
$db->connect("localhost", "root", "");
$db->selectDB('wxbook');

$bi = new Wb_bookinfo($db);
$bi-> insertBookInfo($bookInfo);
$db->closeDB();

处理mysql入库动作的类代码如下:

class Wb_db
{
public $conn = null;
public function connect($host, $user, $password)
{
//$con = mysqli_connect("localhost", "root", "");
$con = mysqli_connect($host, $user, $password);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$this->conn = $con;
return $con;
}
public function selectDB($database)
{
if (!$this->conn) {
die('Could not connect: ' . mysql_error());
return;
}
$program_char = "utf8";
mysqli_select_db($this->conn,$database);
mysqli_query($this->conn,"SET NAMES 'utf8'");
mysqli_set_charset($this->conn,$program_char);
//$charset = mysqli_client_encoding($this->conn);
}
public function queryDB($sql)
{
if (!$this->conn) {
die('Could not connect: ' . mysql_error());
return;
}
$result = mysqli_query($this->conn,$sql);
if (!$result) {
die('Error: ---' . $this->conn->errno.':'.$this->conn->error);
return false;
} else {
echo "excute success";
return $result;
}
}
public function closeDB()
{
if (!$this->conn) {
die('Could not connect: '.mysql_errno.":".mysql_error());
return;
}
mysqli_close($this->conn);
}
}

class Wb_bookinfo
{

public $db = null;
public function __construct($database)
{
$this->db = $database;
}
public function insertBookInfo($bookInfo)
{
if ($bookInfo['title'] == '' or $bookInfo['isbn13'] == '') {
return false;
}
if (is_array($bookInfo['author']) && sizeof($bookInfo['author']) > 0) {
$bookInfo['author'] = $bookInfo['author'][0];
} else {
$bookInfo['author'] = '';
}
if (is_array($bookInfo['translator'])) {
if (count($bookInfo['translator']) > 0) {
$bookInfo['translator'] = $bookInfo['translator'][0];
} else {
$bookInfo['translator'] = '';
}
}
if (mb_strlen($bookInfo['summary'], "UTF-8") > 500) {
$summary = mb_substr($bookInfo['summary'], 0, 499, 'utf-8') . '...';
} else {
$summary = $bookInfo['summary'];
}
#var_dump($bookInfo);
$sql = sprintf("replace INTO wb_bookinfo(isbn10, isbn13,title,author,price,image,translator,
publisher,summary,rating)
VALUES ('%s','%s','%s','%s',%f,'%s','%s','%s','%s',%f)",
$bookInfo["isbn10"],
$bookInfo["isbn13"],
$bookInfo["title"],
$bookInfo["author"],
$bookInfo["price"],
$bookInfo["image"],
$bookInfo["translator"],
$bookInfo["publisher"],
$summary,
$bookInfo['rating']['average']);

$query = $this->db->queryDB($sql);
return $query;
}

}

增加图书信息入功能后,在扫码以后就可以将信息同步存入数据库中,方便后续使用。

你可能感兴趣的:(10 实例讲解微信公众号开发-图书信息保存)