Home >WeChat Applet >WeChat Development >PHP WeChat public account development (2) Baidu BAE construction and database use
WeChat is becoming more and more popular. Let’s start learning WeChat public account development today. Before development, if you already know PHP knowledge and HTML/css and other technologies.
1. Apply for a WeChat public account: Address https://mp.weixin.qq.com/
Before registration, you need to take a bust of your ID card and make sure you can see the ID card information clearly. The photos I took with my iPhone 4s can be used. Don’t beautify the photos, just keep them original. It’s not a blind date.
Fill in the relevant information, as long as it is true. It will be reviewed within 7 working days to see if it passes. You will usually know the result in two or three days. If not, just resubmit. You only have 4 chances in total, so be careful.
One person can apply for two official accounts, because both the mobile phone number and the ID card can only be registered twice.
You need to use your email address to register, so apply in advance. Your email address is used for verification and is also your login number.
Individual users can only apply for a subscription account. Governments, enterprises, and media can apply for service accounts. The two types of development are basically the same, but with better services and more functions, we can only choose a subscription account.
After the application is approved, log in and upload an avatar to your account first. Make sure it is the same as the theme of your account. And can be bound to Weibo and so on.
2 Apply for Baidu BAE, which is similar to Sina SAE. The two operations are similar. BAE is used as an example here.
Application address: http://developer.baidu.com/
Just register as a personal type, there is nothing to say, basically no problem.
After passing the application, you can use Baidu BAE. Baidu bae can build websites or serve as a server platform for developing applications, and provides many interfaces.
Find the management center - "Cloud Platform Management -" Quickly create an application in the upper right corner:
Fill in the application name, select mobile application as the access method, and confirm.
When the application is generated, you can set up the cloud environment for the application, that is, which language to develop it in. Here, choose php.
Get familiar with the backend, which provides many functions. Details will be introduced later, this is just preliminary knowledge!
The above is mainly some preparatory knowledge, and then there is the preparatory knowledge. You can develop WeChat without using a database, but if you want to build some query applications, you may use database operations, so this section mainly involves Baidu BAE The above database table creation, inserting data, modifying data, deleting data, deleting tables, and transaction operations.
Before development, go to bae to create an application, and then add the database MySQL. A database name will be automatically generated, which is a string.
Mysqli is used here for testing, which is similar to MySQL.
$dbname = "tIXZeAfkXSSgYzKuMwGZ";//百度bae数据库 这就是生成的数据库,要替换成自己的 /*从环境变量里取出数据库连接需要的参数*/ $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'); $db= new mysqli($host,$user,$pwd,$dbname,$port); if(mysqli_connect_errno()){ die("coonet error").mysqli_connect_errno(); }else{ echo 'ok'; } $db->query("set names utf8");
This is the Baidu bae database connection, and then you can use mysqli related database methods to operate.
1.Create table
$sql = "create table if not exists t_mysql( id int(10) primary key AUTO_INCREMENT, no int(10), name VARCHAR(255))"; $result = $db->query($sql); if($result){ echo "创建表成功"; }
2.Insert
$sql ="insert into t_mysql(id,no,name) values(null,1,'你好')"; $result = $db->query($sql); if($result){ echo '插入成功'; }
3.Modify
$sql ="update t_mysql set name='重活' where id=1"; $result = $db->query($sql); if($db->affected_rows>0 && $result){ echo '修改成功'; }
4.Delete
$sql ="delete from t_mysql where id=4"; $result = $db->query($sql); if($result && $db->affected_rows>0){ echo '删除成功'; }
5.Query
$sql = "select * from t_mysql"; $result = $db->query($sql); if($result){ $row = $result->fetch_row(); //索引数组 $r[0] print_r($row); }
6. Delete table
$sql = "drop table if exists t_mysql"; $result = $db->query($sql); if($result){ echo "删除表成功"; }
7. Transaction (note that only the innoDB engine supports myisam and does not support transactions)
$db->autocommit(FALSE); $result1 = $db->query("insert into t_mysql values(null,3,'2333')"); $result2 = $db->query("insert into t_mysql values(null,5,'44433')"); if($result1 && $result2){ $db->commit(); echo 'ok'; }else{ $db->rollback(); }
The above is the basic operation of BAE, in fact, it is general PHP Operating the database is enough to build a general WeChat application on it.
File download http://xiazai.jb51.net/201612/yuanma/baemysqli(jb51.net).rar
Outside article
mysql database connection code
<?php $dbname = "tIXZeAfkXSSgYzKuMwGZ";//百度bae数据库 /*从环境变量里取出数据库连接需要的参数*/ $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)); } else{ echo 'ok'; } mysql_query("set names utf8");//设置编码utf8 ?>
The above is the content of PHP WeChat public account development (2) Baidu BAE construction and database use. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!