Home WeChat Applet WeChat Development Database operation for WeChat development

Database operation for WeChat development

May 15, 2017 pm 01:24 PM
Database operations

This article mainly introduces the relevant information on the database operation of WeChat public platform development. Friends who need it can refer to it

1. Introduction

The functions explained earlier Development is completed by simply calling API, without operating the database. In the subsequent development of advanced functions, a database will need to be used, so in this article, a brief introduction to the operation of the MySQL database will be provided for readers' reference.

2. Idea Analysis

Baidu Developer Center provides powerful cloud databases (including MySQL, MongoDB, Redis), in this tutorial, we will demonstrate the operation of the MySQL database that everyone is familiar with, and realize the interaction between WeChat and the database.

It is very simple to use cloud database in BAE application. The name in the database list is the dbname when connecting to the database. The username, password, connection address and port are retrieved through the environment variables in the application.

You can use the standard PHP Mysql or PHP Mysqli extension to access the database. These two extensions are already provided in BAE's PHP and can be used directly by the application.

3. Create BAE MySQL database

3.1 Log in to Baidu Developer Center-> Management Center-> Select Application-> Cloud Environment-> Service Management-> MySQL (Cloud Database) -> Create Database

3.2 Create Database

Note: Each application has only one database that enjoys the 1G free quota, and the other databases do not enjoy the free quota discount. This offer can only be used again if the databasethat has used the free quota is deleted.

3.3 Successfully created

Here you can see the name of the database, which is dbname, which will be used later.

Click "phpMyadmin" to access the database.

3.4 phpMyadmin interface

Create a new data table, enter the table name and number of fields, and click "Execute" to create the table.

3.5 Create a table

Enter the field name and field type. After completing the input, click "Save" below to complete the creation of the table.

3.6 Creation Complete

Modify the id field as the primary key and add AUTO_INCREMENT; modify the from_user field to unique (UNIQUE) to complete the modification of the table.

The table creation operation can also be completed using the following SQL statement:

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`));
Copy after login

phpMyAdmin operation

The creation of the database and data tables ends here. Next, we will write code to explain in detail the use of the database and data tables.

4. Official example (PHP MySQL)

The demo (PHP MySQL) example officially provided by BAE is as follows:

mysql/basic.php file Content

<!--?php
 
require_once &#39;includes/configure.php&#39;;
 
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(){
 
    /*从平台获取查询要连接的数据库名称*/
    $this->database = MYSQLNAME;
 
    /*从环境变量里取出数据库连接需要的参数*/
    $this->host = getenv(&#39;HTTP_BAE_ENV_ADDR_SQL_IP&#39;);
    $this->user = getenv(&#39;HTTP_BAE_ENV_AK&#39;);
    $this->password = getenv(&#39;HTTP_BAE_ENV_SK&#39;);
    $this->port = getenv(&#39;HTTP_BAE_ENV_ADDR_SQL_PORT&#39;);
 
    $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;//执行成功
      }else{
        return 2;//没有行受影响
      }
    }
   
    //$this->mysqli->close();
  }
}
?>
Copy after login

9. Use of test classes

9.1 Test DML operation

Test code:

<!--?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(&#39;David&#39;,&#39;860510&#39;, &#39;abcabc&#39;, &#39;2013-09-27 17:14:28&#39;)";
 
//$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 "执行失败";
}elseif($res==1){
  echo "执行成功";
}else{
  echo "没有行数影响";
}
?>
Copy after login

Test result:

9.2 Test DQL operation

Test code:

<!--?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 &#39;
&#39;;
}
 
$res->free();
?>
Copy after login

Test result:

10. Implement interaction with WeChat (Mysqli extension)

10.1 Pre-operation

A. Introduce the MySQLi_BAE.class.php file

//Introduce the database function file require_once "MySQLi_BAE.class.php";

B. Instantiate the object

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

10.2 Test insert operation

Test code:

$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 );

Test results:

10.3 Test query operation

Test code:

$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);

Test results:

10.4 Test update operation

Test code:

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

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

Test result:

10.5 Test delete operation

Test code:

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

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

Test result:

Interaction test with WeChat was successful.

【Related Recommendations】

1. Special Recommendation:"php Programmer Toolbox" V0.1 version Download

2. WeChat public account platform source code download

3. WeChat Network King v3.4.5 Advanced Commercial Edition WeChat Rubik’s Cube source code

The above is the detailed content of Database operation for WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use CodeIgniter4 framework in php? How to use CodeIgniter4 framework in php? May 31, 2023 pm 02:51 PM

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

How to use Pagoda Panel for MySQL management How to use Pagoda Panel for MySQL management Jun 21, 2023 am 09:44 AM

Pagoda Panel is a powerful panel software that can help us quickly deploy, manage and monitor servers, especially small businesses or individual users who often need to build websites, database management and server maintenance. Among these tasks, MySQL database management is an important job in many cases. So how to use the Pagoda panel for MySQL management? Next, we will introduce it step by step. Step 1: Install Pagoda Panel. Before starting to use Pagoda Panel for MySQL management, you first need to install Pagoda Panel.

How to use PHP scripts to perform database operations in Linux environment How to use PHP scripts to perform database operations in Linux environment Oct 05, 2023 pm 03:48 PM

How to use PHP to perform database operations in a Linux environment. In modern web applications, the database is an essential component. PHP is a popular server-side scripting language that can interact with various databases. This article will introduce how to use PHP scripts for database operations in a Linux environment and provide some specific code examples. Step 1: Install the Necessary Software and Dependencies Before starting, we need to ensure that PHP and related dependencies are installed in the Linux environment. usually

How to use thinkorm to improve database operation efficiency How to use thinkorm to improve database operation efficiency Jul 28, 2023 pm 03:21 PM

How to use thinkorm to improve database operation efficiency With the rapid development of the Internet, more and more applications require a large number of database operations. In this process, the efficiency of database operations becomes particularly important. In order to improve the efficiency of database operations, we can use thinkorm, a powerful ORM framework, to perform database operations. This article will introduce how to use thinkorm to improve the efficiency of database operations and illustrate it through code examples. 1. What is thinkormthi?

How to use the FULL OUTER JOIN function in MySQL to obtain the union of two tables How to use the FULL OUTER JOIN function in MySQL to obtain the union of two tables Jul 26, 2023 pm 05:45 PM

How to use the FULLOUTERJOIN function in MySQL to obtain the union of two tables. In MySQL, the FULLOUTERJOIN function is a powerful join operation that combines inner joins and outer joins. It can be used to get the union of two tables, that is, combine all the data in the two tables into a single result set. This article will introduce the usage of the FULLOUTERJOIN function and provide some sample code to help readers better understand. FULLOUTERJOIN function

Using PDO for Database Operations: A Better Way with PHP Using PDO for Database Operations: A Better Way with PHP Jun 21, 2023 pm 01:36 PM

Using PDO for database operations: A better way with PHP In web development, it is very common to use databases for data storage, management, and query. As a language widely used in Web development, PHP naturally provides a wealth of database operation methods. In PHP, you can use MySQLi, PDO and other extension libraries to perform database operations. Among them, PDO is a very commonly used database operation method and has more advantages than other methods. This article will introduce what PDO is to

How to use Doctrine ORM for database operations in Symfony framework How to use Doctrine ORM for database operations in Symfony framework Jul 29, 2023 pm 04:13 PM

How to use DoctrineORM in Symfony framework for database operations Introduction: Symfony framework is a popular PHP framework that provides many powerful tools and components for building web applications quickly and easily. One of the key components is DoctrineORM, which provides an elegant way to handle database operations. This article will introduce in detail how to use DoctrineORM to perform database operations in the Symfony framework. we will

PHP backend design: database operation and data interaction practice PHP backend design: database operation and data interaction practice Jan 19, 2024 am 10:31 AM

When developing a website, application or system, database operations and data interaction are essential. As a commonly used backend development language, PHP's database operation and data interaction capabilities are also very powerful. This article will introduce some commonly used database operation functions and data interaction practices in PHP. At the same time, we will explain these operations and practices with code examples to facilitate readers to better understand and apply them. 1. Database connection Before performing database operations, you first need to connect to the database. In PHP we can use

See all articles