Sina App Engine (SAE) Introductory tutorial (2)-mysql use

Source: Internet
Author: User

If you do not have an SAE account, please register a new user at http://sae.sina.com.cn. For a specific registration process, see:Sina App Engine (SAE) Introductory tutorial (1) in a regular environment, we can http://php.sinaapp.com/manual/zh/ref.mysql.php PHP's native function to operate MySQL, but due to SAE's environmental problems, the use of master-slave separation technology, so we have a database manipulation to do a package, of course, you can continue to use this way to operate MySQL. You can use the following. First get the database connection host name, account number, password, port. You can run the script on the SAE:

Get connection Information
<? PHP Header ("Content-type:text/html;charset=utf-8"echo "username:".) Sae_mysql_user. " <br> "; Echo "Password:". Sae_mysql_pass. ' <br> '; Echo "Main Library domain name:". Sae_mysql_host_m. " <br> "; Echo "From the Library domain name:". sae_mysql_host_s. " <br> "; Echo "Port". Sae_mysql_port. " <br> "; Echo "Database name:". sae_mysql_db. " <br> ";? >

You can get the following information:

User name:k5nmzy5445 password :lzxkxy0x2iyili3k113iiw1mz5kimlwk33j5wyl1 Main Library domain name : w.rdc.sae.sina.com. cn from library domain name : r.rdc.sae.sina.com. CN Port 3307 Database name : App_lazydemo

Visit http://1.lazydemo.sinaapp.com/saemysql/mysqlcontant.php to view the disguise.

So we have the SAE database connection information, then we can follow the normal way to connect our MySQL, note that it is best not to use the above information directly, but use the constant name provided by the SAE, because this information may be changed, If you change then you may get a failure to connect to the database if you write to death. Here is a general example of how to connect the SAE MySQL.

<?PHP$hostname= Sae_mysql_host_m. ': '.Sae_mysql_port;$dbuser=Sae_mysql_user;$dbpass=Sae_mysql_pass;$dbname=sae_mysql_db;$link=mysql_connect($hostname,$dbuser,$dbpass);if(!$link) {     die(' Could not connect: '.Mysql_error());}Echo' Connected successfully<br/> ';//Select DBmysql_select_db($dbname,$link) or die(' can\ ' t use dbname: '.Mysql_error());Echo' Select db '.$dbname. ' Successfully ';Mysql_close($link);?>

You can access the http://1.lazydemo.sinaapp.com/saemysql/connect_common.php to see the demo and get the results:

Connected Successfullyselect DB App_lazydemo successfully
Using Saemysql

However, it is obvious that the above code is only used in the main library, of course, you can implement the script to achieve read and write separation:) Because this tutorial is for beginners only, it is no longer available here. The master-slave separation provided by SAE is not used, so we recommend using the SAE encapsulated Saemysql operation class to operate the database, compared to the above method, using Saemysql is much simpler, we only need the following script to complete the database connection and database selection,

<? PHP $mysql New Saemysql ();? >

The following is a simple database operation example to demonstrate the use of Saemysql, first create a simple data table (in your own practice can be directly imported into the compressed package mysqldemo.sql), first look at the structure of the table:

IF Not EXISTS ' Mysqldemo ' (  ' id ' int (null auto_increment,  null,    NULL,  KEY  (' id ')) ENGINEDEFAULT Charset=utf8 Collate=utf8_unicode_ci auto_increment=1;

It can be seen that only a simple three field id,content,timeline. Since the current data table is empty, let's first insert a few data, write a loop, the code is as follows:

<?PHP$mysql=NewSaemysql (); for($i= 1;$i< 11;$i++){        $timeline=Date(' Y-m-d h:i:s ', Time()); $content= ' This num is '.$i; $sql= "INSERT into Mysqldemo (content,timeline) VALUES ('$content‘,‘$timeline‘)"; $mysql->runsql ($sql);}//Close DB connection$mysql-closedb ();?>

We access the code that is written to insert the data, and you can see:

The data has been written in. So here's a demonstration of how to use saemysql other functions to manipulate it.

<?PHP$mysql=NewSaemysql ();//Querying single Data$sql= "SELECT * from Mysqldemo limit 1";$result=$mysql->getline ($sql);Var_dump($result);//found that this is already returned as an array.Echo";//querying more than one piece of data$sql= "SELECT * FROM Mysqldemo";$mut _data=$mysql->getdata ($sql);Var_dump($mut _data);//This is the output of a two-dimensional array, the following foreach outputEcho";foreach($mut _data  as $small){        Echo"No".$small[' ID ']. " Content: ".$small[' content ']. ' Timeline: '.$small[' Timeline ']. ' <br> ';}?>

You can see that the output is:

Array (3) {["id"]=> string (1) "1" ["Content"]=> string "This num is1" ["Timeline"]=> string (19) "2012-10-23 1 3:55:21 "}array" {[0]=> Array (3) {["id"]=> string (1) "1" ["Content"]=> string (a) "This num is1" ["Timeline" ]=> string ("2012-10-23 13:55:21"} [1]=> Array (3) {["id"]=> string (1) "2" ["Content"]=> string (a) "this Num IS2 "[" Timeline "]=> string (+)" 2012-10-23 13:55:21 "} [2]=> Array (3) {[" id "]=> string (1)" 3 "[" Content "]=&G T String ("This num is3" ["Timeline"]=> string "2012-10-23 13:55:21"} [3]=> Array (3) {["id"]=> string (1) " 4 "[" Content "]=> string ()" This num IS4 "[" Timeline "]=> string ()" 2012-10-23 13:55:21 "} [4]=> Array (3) {[" I D "]=> string (1)" 5 "[" Content "]=> string" This num is5 "[" Timeline "]=> string (19)" 2012-10-23 13:55:21 "} [5] = = Array (3) {["id"]=> string (1) "6" ["Content"]=> string "This num is6" ["Timeline"]=> string (19) "2012-1 0-23 13:55:21 "} [6]=> Array (3) {["id"]=> string (1) "7" ["Content"]=> string "This num IS7" ["Timeline"]=> string (19) "2012-10  -23 13:55:21 "} [7]=> Array (3) {[" id "]=> string (1)" 8 "[" Content "]=> string" This num is8 "[" Timeline "]=> String "2012-10-23 13:55:21"} [8]=> Array (3) {["id"]=> string (1) "9" ["Content"]=> string (a) "This num is 9 "[" Timeline "]=> string (+)" 2012-10-23 13:55:21 "} [9]=> Array (3) {[" id "]=> string (2)" Ten "[" Content "]=> St Ring ("This num IS10" ["Timeline"]=> string () "2012-10-23 13:55:21"}}no 1 content:this num is1 timeline:2012-10- 13:55:21no 2 content:this num is2 timeline:2012-10-23 13:55:21no 3 content:this num IS3 timeline:2012-10-23 13:55:21No  4 content:this num IS4 timeline:2012-10-23 13:55:21no 5 content:this num is5 timeline:2012-10-23 13:55:21no 6 Content:This Num IS6 timeline:2012-10-23 13:55:21no 7 content:this num IS7 timeline:2012-10-23 13:55:21no 8 content:this num IS8 Timel Ine:2012-10-23 13:55:21no 9 content:this num is9 timeline:2012-10-23 13:55:21no content:this num IS10 timeline:2012-10-23 13:55:21 

can also be accessed directly: http://lazydemo.sinaapp.com/saemysql/query_mysql.php

Learn Saemysql Data reference:
    • Saemysql function Description
    • Official documents
This department code package download:

Click here to download

Sina App Engine (SAE) Introductory tutorial (2)-mysql use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.