微信公众平台开发数据库操作

广告:宝塔Linux面板高效运维的服务器管理软件 点击【 https://www.bt.cn/p/uNLv1L 】立即购买

微信公众平台开发数据库操作

一、简介

前面讲解的功能开发都是简单的调用API 完成的,没有对数据库进行操作。在接下来的高级功能开发中,需要使用到数据库,所以在这一篇中,将对MySQL 数据库的操作做一下简单的介绍,以供读者参考。

二、思路分析

百度开发者中心提供了强大的云数据库(包括MySQL, MongoDB, Redis),在这一节教程中,我们将对大家比较熟悉的MySQL 数据库进行操作演示,实现微信与数据库的交互。

在BAE应用中使用云数据库十分简单,数据库列表中的名称即是连接数据库时的dbname。用户名、密码、连接地址和端口在应用中通过环境变量取出。

可使用标准的PHP Mysql 或PHP Mysqli 扩展访问数据库,BAE的PHP中已提供这两个扩展,应用可直接使用。

官方文档,请参考:ttp://developer.baidu.com/wiki/index.php?title=docs/cplat/rt/mysql

三、创建BAE MySQL数据库

3.1 登陆百度开发者中心 -> 管理中心 -> 选择应用 -> 云环境 -> 服务管理 -> MySQL(云数据库) -> 创建数据库

3.2 创建数据库

注意:每个应用有且只有一个数据库享受1G免费配额,其余数据库均不享受免费配额优惠。只有将已使用免费配额的数据库删除,才能再次使用此项优惠。

3.3 创建成功

在这里可以看到数据库的名称,也就是dbname,后面会使用到。

点击 “phpMyadmin” 访问数据库。

3.4 phpMyadmin界面

新建数据表,输入表名及字段数,点击 “执行” 创建表。

3.5 创建表

输入字段名及字段类型,输入完毕后,点击下面的“保存”,完成表的创建。

3.6 创建完成

修改id 字段为主键并添加AUTO_INCREMENT;修改from_user 字段为唯一(UNIQUE),完成表的修改。

建表操作也可以使用以下SQL语句完成:

CREATE TABLE IF NOT EXISTS `test_mysql` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `from_user` varchar(40) DEFAULT NULL,  `account` varchar(40) DEFAULT NULL,  `password` varchar(40) DEFAULT NULL,  `update_time` datetime DEFAULT NULL,  PRIMARY KEY (`id`),  UNIQUE KEY `from_user` (`from_user`));
登录后复制

phpMyAdmin 操作

数据库及数据表的创建到此结束,下面将编写代码对数据库及数据表的使用做详细讲解。

四、官方示例(PHP MySQL)

BAE 官方提供的demo(PHP MySQL)示例如下:

mysql/basic.php 文件内容

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>
登录后复制

configure.php 文件内容

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>
登录后复制

测试使用:

执行成功。

五、修改成可调用的函数形式(PHP MySQL)

5.1 创建数据表

//创建一个数据库表function _create_table($sql){    mysql_query($sql) or die('创建表失败,错误信息:'.mysql_error());    return "创建表成功";}
登录后复制

5.2 插入数据

//插入数据function _insert_data($sql){      if(!mysql_query($sql)){        return 0;    //插入数据失败    }else{          if(mysql_affected_rows()>0){              return 1;    //插入成功          }else{              return 2;    //没有行受到影响          }    }}
登录后复制

5.3 删除数据

//删除数据function _delete_data($sql){      if(!mysql_query($sql)){        return 0;    //删除失败      }else{          if(mysql_affected_rows()>0){              return 1;    //删除成功          }else{              return 2;    //没有行受到影响          }    }}
登录后复制

5.4 修改数据

//修改数据function _update_data($sql){      if(!mysql_query($sql)){        return 0;    //更新数据失败    }else{          if(mysql_affected_rows()>0){              return 1;    //更新成功;          }else{              return 2;    //没有行受到影响          }    }}
登录后复制

5.5 检索数据

//检索数据function _select_data($sql){    $ret = mysql_query($sql) or die('SQL语句有错误,错误信息:'.mysql_error());    return $ret;}
登录后复制

5.6 删除数据表

//删除表function _drop_table($sql){    mysql_query($sql) or die('删除表失败,错误信息:'.mysql_error());    return "删除表成功";}
登录后复制

将以上函数和连接数据库的代码结合起来,生成mysql_bae.func.php 文件,供下面测试使用。

六、测试MySQL 函数使用

6.1 新建文件dev_mysql.php 在同一目录下并引入mysql_bae.func.php 文件

require_once './mysql_bae.func.php';
登录后复制

6.2 测试创建表

将上面使用phpMyAdmin 创建的test_mysql 表删除,测试语句如下:

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>0
登录后复制

测试正确结果:

到phpMyAdmin中查看

故意将SQL语句写错

测试错误结果:

6.3 测试插入数据

测试语句如下:

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>1
登录后复制

测试结果:

6.4 测试更新数据

测试语句如下:

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>2
登录后复制

测试结果:

再次更新:

6.5 测试删除数据

测试语句如下:

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>3
登录后复制

测试结果:

再次删除:

6.6 测试检索数据

再次执行上面的插入操作做检索测试,测试语句如下:

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>4
登录后复制

测试结果:

6.7 测试删除表

测试语句如下:

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>5
登录后复制

测试结果:

MySQL 函数测试全部成功。

七、实现与微信的交互(Mysql 扩展)

保证数据库中存在test_msyql表,这里测试微信对MySQL数据库的增删改查操作,不考虑特殊情况,只按照下面的方法测试:

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>6
登录后复制

7.1 引入mysql_bae.func.php 文件

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>7
登录后复制

7.2 前置操作

A. 将输入的语句拆分成数组,以“+”号分隔

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>8
登录后复制

B. 获取当前时间

<?php/** * MySQL示例,通过该示例可熟悉BAE平台MySQL的使用(CRUD) */require_once("../configure.php");    /*替换为你自己的数据库名(可从管理中心查看到)*/    $dbname = MYSQLNAME;         /*从环境变量里取出数据库连接需要的参数*/    $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');    $user = getenv('HTTP_BAE_ENV_AK');    $pwd = getenv('HTTP_BAE_ENV_SK');        /*接着调用mysql_connect()连接服务器*/    $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);    if(!$link) {      die("Connect Server Failed: " . mysql_error());    }    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/    if(!mysql_select_db($dbname,$link)) {      die("Select Database Failed: " . mysql_error($link));    }    /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/    /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/         /**    * 接下来就可以使用其它标准php mysql函数操作进行数据库操作    */        //创建一个数据库表    $sql = "create table if not exists test_mysql(            id int primary key auto_increment,            no int,             name varchar(1024),            key idx_no(no))";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Create Table Failed: " . mysql_error($link));    } else {        echo "Create Table Succeed<br />";    }        //插入数据    $sql = "insert into test_mysql(no, name) values(2007,'this is a test message'),            (2008,'this is another test message'),            (2009,'xxxxxxxxxxxxxx')";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Insert Failed: " . mysql_error($link));    } else {        echo "Insert Succeed<br />";    }        //删除数据    $sql = "delete from test_mysql where no = 2008";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Delete Failed: " . mysql_error($link));    } else {        echo "Delete  Succeed<br />";    }        //修改数据    $sql = "update test_mysql set name = 'yyyyyy' where no = 2009";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Update Failed: " . mysql_error($link));    } else {        echo "Update Succeed<br />";    }            //检索数据    $sql = "select id,no,name from test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Select Failed: " . mysql_error($link));    } else {        echo "Select Succeed<br />";        while ($row = mysql_fetch_assoc($ret)) {            echo "{$row['id']} {$row['no']} {$row['name']}<br />";        }    }        //删除表    $sql = "drop table if exists test_mysql";    $ret = mysql_query($sql, $link);    if ($ret === false) {        die("Drop Table Failed: " . mysql_error($link));    } else {        echo "Drop Table Succeed<br />";    }?>9
登录后复制

C. 判断用户是否已经绑定

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>0
登录后复制

7.3 测试插入操作

测试代码:

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>1
登录后复制

测试结果:

7.4 测试查询操作

测试代码:

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>2
登录后复制

测试结果:

7.5 测试更新操作

测试代码:

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>3
登录后复制

测试结果:

7.6 测试删除操作

测试代码:

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>4
登录后复制

测试结果:

与微信的交互测试成功。

八、PHP Mysqli 扩展,封装成类

将Mysqli 扩展封装成类使用,代码如下:

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>5
登录后复制

九、测试类的使用

9.1 测试DML操作

测试代码:

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>6
登录后复制

测试结果:

9.2 测试DQL操作

测试代码:

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>7
登录后复制

测试结果:

十、实现与微信的交互(Mysqli 扩展)

10.1 前置操作

A. 引入MySQLi_BAE.class.php 文件

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>8
登录后复制

B. 实例化对象

<?php    /***配置数据库名称***/    define("MYSQLNAME", "qzMlSkByflhScPCOFtax");?>9
登录后复制

10.2 测试插入操作

测试代码:

//创建一个数据库表function _create_table($sql){    mysql_query($sql) or die('创建表失败,错误信息:'.mysql_error());    return "创建表成功";}0
登录后复制

测试结果:

10.3 测试查询操作

测试代码:

//创建一个数据库表function _create_table($sql){    mysql_query($sql) or die('创建表失败,错误信息:'.mysql_error());    return "创建表成功";}1
登录后复制

测试结果:

10.4 测试更新操作

测试代码:

//创建一个数据库表function _create_table($sql){    mysql_query($sql) or die('创建表失败,错误信息:'.mysql_error());    return "创建表成功";}2
登录后复制

测试结果:

10.5 测试删除操作

测试代码:

//创建一个数据库表function _create_table($sql){    mysql_query($sql) or die('创建表失败,错误信息:'.mysql_error());    return "创建表成功";}3
登录后复制

测试结果:

与微信交互测试成功。

更多微信公众平台开发数据库操作相关文章请关注PHP中文网!

广告:SSL证书一年128.66元起,点击购买~~~

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。
作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:php int类型是什么意思
下一篇:CSS如何使用精灵图

发表评论

关闭广告
关闭广告