国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

微信公眾平臺開發(fā) 數(shù)據(jù)庫操作

2024-07-24 13:10:57
字體:
來源:轉載
供稿:網友

一、簡介

前面講解的功能開發(fā)都是簡單的調用API 完成的,沒有對數(shù)據(jù)庫進行操作。在接下來的高級功能開發(fā)中,需要使用到數(shù)據(jù)庫,所以在這一篇中,將對MySQL 數(shù)據(jù)庫的操作做一下簡單的介紹,以供讀者參考。

二、思路分析

百度開發(fā)者中心提供了強大的云數(shù)據(jù)庫(包括MySQL, MongoDB, Redis),在這一節(jié)教程中,我們將對大家比較熟悉的MySQL 數(shù)據(jù)庫進行操作演示,實現(xiàn)微信與數(shù)據(jù)庫的交互。

在BAE應用中使用云數(shù)據(jù)庫十分簡單,數(shù)據(jù)庫列表中的名稱即是連接數(shù)據(jù)庫時的dbname。用戶名、密碼、連接地址和端口在應用中通過環(huán)境變量取出。

可使用標準的PHP Mysql 或PHP Mysqli 擴展訪問數(shù)據(jù)庫,BAE的PHP中已提供這兩個擴展,應用可直接使用。

三、創(chuàng)建BAE MySQL數(shù)據(jù)庫

3.1 登陸百度開發(fā)者中心 -> 管理中心 -> 選擇應用 -> 云環(huán)境 -> 服務管理 -> MySQL(云數(shù)據(jù)庫) -> 創(chuàng)建數(shù)據(jù)庫

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

3.2 創(chuàng)建數(shù)據(jù)庫

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

注意:每個應用有且只有一個數(shù)據(jù)庫享受1G免費配額,其余數(shù)據(jù)庫均不享受免費配額優(yōu)惠。只有將已使用免費配額的數(shù)據(jù)庫刪除,才能再次使用此項優(yōu)惠。

3.3 創(chuàng)建成功

在這里可以看到數(shù)據(jù)庫的名稱,也就是dbname,后面會使用到。

點擊 “phpMyadmin” 訪問數(shù)據(jù)庫。

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

3.4 phpMyadmin界面

新建數(shù)據(jù)表,輸入表名及字段數(shù),點擊 “執(zhí)行” 創(chuàng)建表。

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

3.5 創(chuàng)建表

輸入字段名及字段類型,輸入完畢后,點擊下面的“保存”,完成表的創(chuàng)建。

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

3.6 創(chuàng)建完成

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

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

建表操作也可以使用以下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 操作

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

數(shù)據(jù)庫及數(shù)據(jù)表的創(chuàng)建到此結束,下面將編寫代碼對數(shù)據(jù)庫及數(shù)據(jù)表的使用做詳細講解。

四、官方示例(PHP MySQL)

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

mysql/basic.php 文件內容

<!--?php/** * MySQL示例,通過該示例可熟悉BAE平臺MySQL的使用(CRUD) */require_once("../configure.php");  /*替換為你自己的數(shù)據(jù)庫名(可從管理中心查看到)*/  $dbname = MYSQLNAME;     /*從環(huán)境變量里取出數(shù)據(jù)庫連接需要的參數(shù)*/  $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()選中需要連接的數(shù)據(jù)庫*/  if(!mysql_select_db($dbname,$link)) {   die("Select Database Failed: " . mysql_error($link));  }  /*至此連接已完全建立,就可對當前數(shù)據(jù)庫進行相應的操作了*/  /*!!!注意,無法再通過本次連接調用mysql_select_db來切換到其它數(shù)據(jù)庫了!!!*/  /* 需要再連接其它數(shù)據(jù)庫,請再使用mysql_connect+mysql_select_db啟動另一個連接*/     /**  * 接下來就可以使用其它標準php mysql函數(shù)操作進行數(shù)據(jù)庫操作  */     //創(chuàng)建一個數(shù)據(jù)庫表  $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 /-->";  }     //插入數(shù)據(jù)  $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";  }     //刪除數(shù)據(jù)  $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";  }     //修改數(shù)據(jù)  $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";  }        //檢索數(shù)據(jù)  $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";    while ($row = mysql_fetch_assoc($ret)) {      echo "{$row['id']} {$row['no']} {$row['name']}";    }  }     //刪除表  $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";  }  ?>

configure.php 文件內容

<!--?php   /***配置數(shù)據(jù)庫名稱***/  define("MYSQLNAME", "qzMlSkByflhScPCOFtax"); ?-->

測試使用:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

執(zhí)行成功。 

五、修改成可調用的函數(shù)形式(PHP MySQL)

5.1 創(chuàng)建數(shù)據(jù)表

//創(chuàng)建一個數(shù)據(jù)庫表function _create_table($sql){  mysql_query($sql) or die('創(chuàng)建表失敗,錯誤信息:'.mysql_error());  return "創(chuàng)建表成功";}

5.2 插入數(shù)據(jù)

//插入數(shù)據(jù)function _insert_data($sql){   if(!mysql_query($sql)){    return 0;  //插入數(shù)據(jù)失敗  }else{     if(mysql_affected_rows()>0){       return 1;  //插入成功     }else{       return 2;  //沒有行受到影響     }  }}

5.3 刪除數(shù)據(jù)

//刪除數(shù)據(jù)function _delete_data($sql){   if(!mysql_query($sql)){    return 0;  //刪除失敗   }else{     if(mysql_affected_rows()>0){       return 1;  //刪除成功     }else{       return 2;  //沒有行受到影響     }  }}

5.4 修改數(shù)據(jù)

//修改數(shù)據(jù)function _update_data($sql){   if(!mysql_query($sql)){    return 0;  //更新數(shù)據(jù)失敗  }else{     if(mysql_affected_rows()>0){       return 1;  //更新成功;     }else{       return 2;  //沒有行受到影響     }  }}

5.5 檢索數(shù)據(jù)

//檢索數(shù)據(jù)function _select_data($sql){  $ret = mysql_query($sql) or die('SQL語句有錯誤,錯誤信息:'.mysql_error());  return $ret;}

5.6 刪除數(shù)據(jù)表

//刪除表function _drop_table($sql){  mysql_query($sql) or die('刪除表失敗,錯誤信息:'.mysql_error());  return "刪除表成功";}

將以上函數(shù)和連接數(shù)據(jù)庫的代碼結合起來,生成mysql_bae.func.php 文件,供下面測試使用。

六、測試MySQL 函數(shù)使用

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

require_once './mysql_bae.func.php';

6.2 測試創(chuàng)建表

將上面使用phpMyAdmin 創(chuàng)建的test_mysql 表刪除,測試語句如下:

//創(chuàng)建表$create_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`))"; echo _create_table($create_sql);

測試正確結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

到phpMyAdmin中查看

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

故意將SQL語句寫錯

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

測試錯誤結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

6.3 測試插入數(shù)據(jù)

測試語句如下:

//插入數(shù)據(jù)$insert_sql = "insert into test_mysql(from_user, account, password, update_time) values('David','860510', 'abcabc', '2013-09-29 17:14:28')"; $res = _insert_data($insert_sql);if($res == 1){  echo "插入成功";}else{  echo "插入失敗";}

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

6.4 測試更新數(shù)據(jù)

測試語句如下:

//更新數(shù)據(jù)$update_sql = "update test_mysql set account = 860512 where account = 860510"; $res = _update_data($update_sql);if($res == 1){  echo "更新成功";}elseif($res == 0){  echo "更新失敗";}elseif($res == 2){  echo "沒有行受到影響";}

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

再次更新:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

6.5 測試刪除數(shù)據(jù)

測試語句如下:

//刪除數(shù)據(jù)$delete_sql = "delete from test_mysql where account = 860512"; $res = _delete_data($delete_sql);if($res == 1){  echo "刪除成功";}elseif($res == 0){  echo "刪除失敗";}elseif($res == 2){  echo "沒有該條記錄";}

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

再次刪除:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

6.6 測試檢索數(shù)據(jù)

再次執(zhí)行上面的插入操作做檢索測試,測試語句如下:

//檢索數(shù)據(jù)$select_sql = "select * from test_mysql"; $result = _select_data($select_sql); while($rows = mysql_fetch_array($result,MYSQL_ASSOC)){   echo $rows[id]."--".$rows[from_user]."--".$rows[account]."--".$rows[password]."--".$rows[update_time];  echo ""; }

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

6.7 測試刪除表

測試語句如下:

//刪除表$drop_sql = "drop table if exists test_mysql";

echo _drop_table($drop_sql);

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

MySQL 函數(shù)測試全部成功。

七、實現(xiàn)與微信的交互(Mysql 擴展)

保證數(shù)據(jù)庫中存在test_msyql表,這里測試微信對MySQL數(shù)據(jù)庫的增刪改查操作,不考慮特殊情況,只按照下面的方法測試:

1. 綁定+賬戶+密碼如:綁定+860512+abc123 2. 查詢如:查詢 3. 修改+舊密碼+新密碼如:修改+abc123+123456 4. 刪除如:刪除

7.1 引入mysql_bae.func.php 文件

//引入數(shù)據(jù)庫函數(shù)文件

require_once 'mysql_bae.func.php';

7.2 前置操作

A. 將輸入的語句拆分成數(shù)組,以“+”號分隔

$keywords = explode("+",$keyword);

B. 獲取當前時間

//獲取當前時間$nowtime=date("Y-m-d G:i:s");

C. 判斷用戶是否已經綁定

//判斷是否已經綁定$select_sql="SELECT id from test_mysql WHERE from_user='$fromUsername'";$res=_select_data($select_sql);$rows=mysql_fetch_array($res, MYSQL_ASSOC);if($rows[id] <> ''){    $user_flag='y';     }

7.3 測試插入操作

測試代碼:

if(trim($keywords[0] == '綁定')){  if($user_flag <> 'y'){    $insert_sql="INSERT INTO test_mysql(from_user, account, password, update_time) VALUES('$fromUsername','$keywords[1]','$keywords[2]','$nowtime')";    $res = _insert_data($insert_sql);    if($res == 1){      $contentStr = "綁定成功";    }elseif($res == 0){      $contentStr = "綁定失敗";    }  }else{    $contentStr = "該賬戶已綁定";  }}

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

7.4 測試查詢操作

測試代碼:

if(trim($keywords[0] == '查詢')){  $select_sql="SELECT * FROM test_mysql WHERE from_user='$fromUsername'";  $select_res=_select_data($select_sql);  $rows=mysql_fetch_assoc($select_res);  if($rows[id] <> ''){  $contentStr="賬戶:$rows[account]/n"."密碼:$rows[password]/n"."From_user:$rows[from_user]/n"."更新時間:$rows[update_time]";  }else{  $contentStr="您還未綁定賬戶,查詢不到相關信息,請先綁定,謝謝!";  }}

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

7.5 測試更新操作

測試代碼:

if(trim($keywords[0] == "修改")){  $old_password=$keywords[1];  $new_password=$keywords[2];  $select_password_sql="SELECT * FROM test_mysql WHERE from_user='$fromUsername'";  $select_res=_select_data($select_password_sql);  $rows=mysql_fetch_assoc($select_res);  if($old_password == $rows[password]){    $update_sql="UPDATE test_mysql SET password='$new_password' WHERE from_user='$fromUsername'";    $res = _update_data($update_sql);    if($res == 1){      $contentStr = "修改成功";    }elseif($res == 0){      $contentStr = "修改失敗";    }  }else{    $contentStr = "原密碼有誤,請確認后重試";  }}

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

7.6 測試刪除操作

測試代碼:

if(trim($keywords[0] == "刪除")){  $delete_sql="DELETE FROM test_mysql WHERE from_user='$fromUsername'";  $res = _delete_data($delete_sql);  if($res == 1){    $contentStr = "刪除成功";  }elseif($res == 0){    $contentStr = "刪除失敗";  }}

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

與微信的交互測試成功。

八、PHP Mysqli 擴展,封裝成類

將Mysqli 擴展封裝成類使用,代碼如下:

<!--?php require_once 'includes/configure.php'; class MySQLi_BAE{   private $mysqli;  private $host;  private $user;  private $password;  private $port;  private $database;   //在類之外訪問私有變量時使用  function __get($property_name){    if(isset($this--->$property_name)){      return($this->$property_name);    }else{      return(NULL);    }    }   function __set($property_name, $value){    $this->$property_name=$value;  }   function __construct(){     /*從平臺獲取查詢要連接的數(shù)據(jù)庫名稱*/    $this->database = MYSQLNAME;     /*從環(huán)境變量里取出數(shù)據(jù)庫連接需要的參數(shù)*/    $this->host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');    $this->user = getenv('HTTP_BAE_ENV_AK');    $this->password = getenv('HTTP_BAE_ENV_SK');    $this->port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');     $this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->database, $this->port);    if($this->mysqli->connect_error){      die("Connect Server Failed:".$this->mysqli->error);    }         $this->mysqli->query("set names utf8");  }   //dql statement  function execute_dql($query){         $res = $this->mysqli->query($query) or die("操作失敗".$this->mysqli->error);    return $res;         //$this->mysqli->close();  }   //dml statement  function execute_dml($query){         $res = $this->mysqli->query($query) or die("操作失敗".$this->mysqli->error);         if(!$res){      return 0;//失敗    }else{      if($this->mysqli->affected_rows > 0){        return 1;//執(zhí)行成功      }else{        return 2;//沒有行受影響      }    }       //$this->mysqli->close();  }}?>

九、測試類的使用

9.1 測試DML操作

測試代碼:

<!--?php require_once "MySQLi_BAE.class.php"; $mysqli_BAE=new MySQLi_BAE();  //**************dml*******************$sql="insert into test_mysql (from_user, account, password, update_time) values('David','860510', 'abcabc', '2013-09-27 17:14:28')"; //$sql="update test_mysql set account = 860512 where account = 860510"; //$sql="delete from test_mysql where account = 860512"; $res=$mysqli_BAE--->execute_dml($sql); if($res==0){  echo "執(zhí)行失敗";}elseif($res==1){  echo "執(zhí)行成功";}else{  echo "沒有行數(shù)影響";}?>

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

9.2 測試DQL操作

測試代碼:

<!--?php require_once "MySQLi_BAE.class.php"; $mysqli_BAE=new MySQLi_BAE(); //**************dql******************$sql="select * from test_mysql"; $res=$mysqli_BAE--->execute_dql($sql); while($row=$res->fetch_row()){     foreach($row as $key=>$val){    echo "$val--";  }  echo '';} $res->free();?>

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

十、實現(xiàn)與微信的交互(Mysqli 擴展)

10.1 前置操作

A. 引入MySQLi_BAE.class.php 文件

//引入數(shù)據(jù)庫函數(shù)文件require_once "MySQLi_BAE.class.php";

B. 實例化對象

public function __construct(){ $this->mysqli_BAE=new MySQLi_BAE();}

10.2 測試插入操作

測試代碼:

$insert_sql="INSERT INTO test_mysql(from_user, account, password, update_time) VALUES('$fromUsername',

'$keywords[1]','$keywords[2]','$nowtime')";$res = $this->mysqli_BAE->execute_dml($insert_sql);

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

10.3 測試查詢操作

測試代碼:

$select_sql="SELECT * FROM test_mysql WHERE from_user='$fromUsername'";

$select_res=$this->mysqli_BAE->execute_dql($select_sql);$rows=$select_res->fetch_array(MYSQLI_ASSOC);

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

10.4 測試更新操作

測試代碼:

$update_sql="UPDATE test_mysql SET password='$new_password' WHERE from_user='$fromUsername'";

$res = $this->mysqli_BAE->execute_dml($update_sql);

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

10.5 測試刪除操作

測試代碼:

$delete_sql="DELETE FROM test_mysql WHERE from_user='$fromUsername'";

$res = $this->mysqli_BAE->execute_dml($delete_sql);

 

測試結果:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

與微信交互測試成功。 

十一、完整代碼獲取

請訪問 樂思樂享 官方論壇

URL:http://pan.baidu.com/s/1c0s3Jby

十二、關注

請關注 卓錦蘇州 微信公眾帳號,卓錦蘇州 基于BAE 平臺開發(fā),針對于主流的微信功能進行開發(fā)測試。

您可以關注 卓錦蘇州 公眾帳號進行功能測試,以及獲取新的應用開發(fā)。

1. 登錄微信客戶端,通訊錄 -> 添加朋友 -> 查找公眾號 -> zhuojinsz,查找并關注。

2. 掃描二維碼:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

卓錦蘇州 功能列表:

微信公眾平臺開發(fā),數(shù)據(jù)庫操作,數(shù)據(jù)庫操作詳解

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到MYSQL教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 于田县| 新营市| 海丰县| 襄城县| 荥经县| 庆安县| 垣曲县| 平乡县| 郓城县| 怀集县| 建水县| 敦煌市| 峨山| 甘泉县| 福贡县| 望城县| 旬阳县| 文昌市| 张家港市| 郓城县| 富蕴县| 衡山县| 房山区| 林口县| 双流县| 万年县| 攀枝花市| 安新县| 巩义市| 棋牌| 扶余县| 余江县| 普兰县| 合阳县| 衡南县| 吉木乃县| 湛江市| 吉木萨尔县| 苏尼特左旗| 白朗县| 贡嘎县|