In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to achieve database operation in Wechat development. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
I. brief introduction
The function development described above is done simply by calling API, and there is no operation on the database. In the next advanced function development, we need to use the database, so in this article, we will make a brief introduction to the operation of the MySQL database for readers' reference.
Second, train of thought analysis
Baidu developer Center provides powerful cloud databases (including MySQL, MongoDB, Redis). In this tutorial, we will demonstrate the operation of the familiar MySQL database to achieve the interaction between Wechat and the database.
It is very simple to use cloud database in BAE applications. The name in the database list is the dbname when connecting to the database. The user name, password, connection address, and port are taken out through the environment variable in the application.
The database can be accessed using standard PHP Mysql or PHP Mysqli extensions, which are available in BAE's PHP and can be used directly by applications.
Third, create a BAE MySQL database
3.1 Log in to Baidu developer Center-> Administration Center-> Select applications-> Cloud Environment-> Service Management-> MySQL (Cloud Database)-> create a database
3.2 create a database
Note: each application has and only one database enjoys a 1G free quota, while the rest of the database does not enjoy a free quota discount. You can use this offer again only if you delete the database that has already used the free quota.
3.3 created successfully
Here you can see the name of the database, that 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 the number of fields, and click "execute" to create the table.
3.5 create a table
Enter the field name and field type, and then click "Save" below to complete the creation of the table.
3.6 creation completed
Modify the id field as the primary key and add AUTO_INCREMENT; to make the from_user field unique (UNIQUE) to complete the modification of the table.
The table creation operation can also be done 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, `passt` varchar (40) DEFAULT NULL, `password` varchar (40) DEFAULT NULL, `update_ time` datetime DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `from_ user` (`from_ user`))
PhpMyAdmin operation
This is the end of the creation of the database and data table. The following code will be written to explain in detail the use of the database and data table.
IV. Official example (PHP MySQL)
An example of demo (PHP MySQL) officially provided by BAE is as follows:
Mysql/basic.php file content
$property_name) {return ($this- > $property_name);} else {return (NULL);}} function set ($property_name, $value) {$this- > $property_name=$value;} function construct () {/ * get the name of the database to be connected from the platform * / $this- > database = MYSQLNAME / * extract the parameters required for database connection from the environment variables * / $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) {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 Return $res; / / $this- > mysqli- > close ();} / / dml statement function execute_dml ($query) {$res = $this- > mysqli- > query ($query) or die ("Operation failure". $this- > mysqli- > error); if (! $res) {return 0bat / failure} else {if ($this- > mysqli- > affected_rows > 0) {return 1 / / successfully executed} else {return 2 racket / No rows affected}} / / $this- > mysqli- > close ();}}? >
IX. The use of test classes
9.1 Test DML operation
Test the code:
Execute_dml ($sql); if ($res==0) {echo "execution failed";} elseif ($res==1) {echo "execution success";} else {echo "No Row count effect";}? >
Test results:
9.2 Test DQL operation
Test the code:
Execute_dql ($sql); while ($row=$res- > fetch_row ()) {foreach ($row as $key= > $val) {echo "$val--";} echo'';} $res- > free ();? >
Test results:
Implement interaction with Wechat (Mysqli extension)
10.1 pre-operation
a. Import MySQLi_BAE.class.php file
/ / introduce the database function file require_once "MySQLi_BAE.class.php"
b. Instantiate object
Public function construct () {$this- > mysqli_BAE=new MySQLi_BAE ();}
10.2 Test insert operation
Test the 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 the 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 the code:
$update_sql= "UPDATE test_mysql SET password='$new_password' WHERE from_user='$fromUsername'"
$res = $this- > mysqli_BAE- > execute_dml ($update_sql)
Test results:
10.5 Test delete operation
Test the code:
$delete_sql= "DELETE FROM test_mysql WHERE from_user='$fromUsername'"
$res = $this- > mysqli_BAE- > execute_dml ($delete_sql)
Test results:
The interaction test with Wechat was successful.
This is the end of the article on "how to achieve database operation in Wechat development". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.