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

Source: Internet
Author: User
<span id="Label3"></p><p><p>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 native function to operate mysql, but because of 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:</p></p>Get connection Information<pre><pre><? <span style="color: #000000;">PHP</span> <span style="color: #008080;">Header</span> ("content-type:text/html;charset=utf-8"<span style="color: #000000;"></span><span style="color: #0000ff;">echo</span> "username:".) Sae_mysql_user. " <br> "<span style="color: #000000;">;</span> <span style="color: #0000ff;">Echo</span> "password:". Sae_mysql_pass. ' <br> '<span style="color: #000000;">;</span> <span style="color: #0000ff;">Echo</span> "main Library Domain name:". Sae_mysql_host_m. " <br> "<span style="color: #000000;">;</span> <span style="color: #0000ff;">Echo</span> "from the library domain name:". Sae_mysql_host_s. " <br> "<span style="color: #000000;">;</span> <span style="color: #0000ff;">Echo</span> "port". Sae_mysql_port. " <br> "<span style="color: #000000;">;</span> <span style="color: #0000ff;">Echo</span> "database name:". Sae_mysql_db. " <br> "<span style="color: #000000;">;</span>? ></pre></pre><p><p>You can get the following information:</p></p><pre><pre>User name:<span style="color: #000000;">k5nmzy5445 password</span> :<span style="color: #000000;">lzxkxy0x2iyili3k113iiw1mz5kimlwk33j5wyl1 Main Library domain name</span> : w.rdc.sae.sina.com. <span style="color: #000000;">cn from library domain name</span> : r.rdc.sae.sina.com. <span style="color: #000000;">CN Port 3307 Database name</span> : App_lazydemo</pre></pre><p><p>Visit http://1.lazydemo.sinaapp.com/saemysql/mysqlcontant.php to view the Disguise.</p></p><p><p>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.</p></p><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #800080;"><span style="color: #800080;">$hostname</span></span>= Sae_mysql_host_m. ': '.<span style="color: #000000;"><span style="color: #000000;">sae_mysql_port;</span></span><span style="color: #800080;"><span style="color: #800080;">$dbuser</span></span>=<span style="color: #000000;"><span style="color: #000000;">sae_mysql_user;</span></span><span style="color: #800080;"><span style="color: #800080;">$dbpass</span></span>=<span style="color: #000000;"><span style="color: #000000;">sae_mysql_pass;</span></span><span style="color: #800080;"><span style="color: #800080;">$dbname</span></span>=<span style="color: #000000;"><span style="color: #000000;">sae_mysql_db;</span></span><span style="color: #800080;"><span style="color: #800080;">$link</span></span>=<span style="color: #008080;"><span style="color: #008080;">mysql_connect</span></span>(<span style="color: #800080;"><span style="color: #800080;">$hostname</span></span>,<span style="color: #800080;"><span style="color: #800080;">$dbuser</span></span>,<span style="color: #800080;"><span style="color: #800080;">$dbpass</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #800080;"><span style="color: #800080;">$link</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> die</span>(' Could not connect: '.<span style="color: #008080;"><span style="color: #008080;">Mysql_error</span></span><span style="color: #000000;"><span style="color: #000000;">());}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span>' Connected successfully<br/> '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Select DB</span></span><span style="color: #008080;"><span style="color: #008080;">mysql_select_db</span></span>(<span style="color: #800080;"><span style="color: #800080;">$dbname</span></span>,<span style="color: #800080;"><span style="color: #800080;">$link</span></span>) or<span style="color: #0000ff;"><span style="color: #0000ff;"></span> die</span>(' can\ ' t use dbname: '.<span style="color: #008080;"><span style="color: #008080;">Mysql_error</span></span><span style="color: #000000;"><span style="color: #000000;">());</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span>' Select db '.<span style="color: #800080;"><span style="color: #800080;">$dbname</span></span>. ' Successfully '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;">Mysql_close</span></span>(<span style="color: #800080;"><span style="color: #800080;">$link</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span>?></pre><p><p>You can access the http://1.lazydemo.sinaapp.com/saemysql/connect_common.php to see the demo and get the Results:</p></p><pre><pre><span style="color: #000000;">Connected Successfullyselect DB App_lazydemo successfully</span></pre></pre>Using Saemysql<p><p>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,</p></p><pre><pre><? <span style="color: #000000;">PHP</span> <span style="color: #800080;">$mysql</span> <span style="color: #0000ff;">New</span> <span style="color: #000000;">saemysql ();</span>? ></pre></pre><p><p>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:</p></p><pre><pre><span style="color: #0000ff;">IF</span> not <span style="color: #000000;">EXISTS ' Mysqldemo ' ( ' id ' int (</span><span style="color: #0000ff;">null</span> auto_increment,<span style="color: #000000;"> </span><span style="color: #0000ff;">null</span>, <span style="color: #000000;"> </span> <span style="color: #0000ff;">NULL</span><span style="color: #000000;">, </span><span style="color: #008080;">KEY</span> <span style="color: #000000;"> (' ID ')) ENGINE</span><span style="color: #0000ff;">DEFAULT</span> Charset=utf8 Collate=utf8_unicode_ci auto_increment=1;</pre></pre><p><p>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:</p></p><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #800080;"><span style="color: #800080;">$mysql</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Saemysql ();</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #800080;"><span style="color: #800080;">$i</span></span>= 1;<span style="color: #800080;"><span style="color: #800080;">$i</span></span>< 11;<span style="color: #800080;"><span style="color: #800080;">$i</span></span>++<span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #800080;"><span style="color: #800080;">$timeline</span></span>=<span style="color: #008080;"><span style="color: #008080;">Date</span></span>(' y-m-d h:i:s ',<span style="color: #008080;"><span style="color: #008080;"></span> time</span><span style="color: #000000;"><span style="color: #000000;">()); </span></span><span style="color: #800080;"><span style="color: #800080;">$content</span></span>= ' This num is '.<span style="color: #800080;"><span style="color: #800080;">$i</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$sql</span></span>= "insert into Mysqldemo (content,timeline) values ('<span style="color: #800080;"><span style="color: #800080;">$content</span></span>‘,‘<span style="color: #800080;"><span style="color: #800080;">$timeline</span></span>‘)"<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$mysql</span></span>->runsql (<span style="color: #800080;"><span style="color: #800080;">$sql</span></span><span style="color: #000000;"><span style="color: #000000;">);}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Close DB connection</span></span><span style="color: #800080;"><span style="color: #800080;">$mysql</span></span>-<span style="color: #000000;"><span style="color: #000000;">closedb ();</span></span>?></pre><p><p>We access the code that is written to insert the data, and you can see:</p></p><p><p></p></p><p><p>The data has been written IN. So here's a demonstration of how to use saemysql other functions to manipulate it.</p></p><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #800080;"><span style="color: #800080;">$mysql</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Saemysql ();</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Querying single Data</span></span><span style="color: #800080;"><span style="color: #800080;">$sql</span></span>= "select * from Mysqldemo limit 1"<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$result</span></span>=<span style="color: #800080;"><span style="color: #800080;">$mysql</span></span>->getline (<span style="color: #800080;"><span style="color: #800080;">$sql</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;">Var_dump</span></span>(<span style="color: #800080;"><span style="color: #800080;">$result</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">found that this is already returned as an array.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span>"
Related Article

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.