Example of SaeMysql operation

Source: Internet
Author: User

Sina sae official documentation: http://apidoc.sinaapp.com/sae/SaeMysql.html

Class SaeMysql implementation: http://apidoc.sinaapp.com/__filesource/fsource_sae__saemysql.class.php.html

The official Sina documentation provides a simple example:

<?php$mysql = new SaeMysql(); $sql = "SELECT * FROM `user` LIMIT 10";$data = $mysql->getData( $sql );$name = strip_tags( $_REQUEST['name'] );$age = intval( $_REQUEST['age'] );$sql = "INSERT  INTO `user` ( `name` , `age` , `regtime` ) VALUES ( '"  . $mysql->escape( $name ) . "' , '" . intval( $age ) . "' , NOW() ) ";$mysql->runSql( $sql );if( $mysql->errno() != 0 ){    die( "Error:" . $mysql->errmsg() );} $mysql->closeDb();?>
However, I think it is not detailed enough, especially for beginners, maybe this is the style of Daniel. The example is concise and concise.

We generally add, delete, modify, and query databases.

The addition, deletion, and modification operations all execute SQL statements and check whether the statements are correctly executed based on the error code;

Query operations are generally divided into two types:

You can obtain a query record, which is commonly used to query the user name and password during login. The corresponding method getLine () in the SaeMysql class returns a one-dimensional array;

Query to obtain multiple records. The returned result is a two-dimensional array, which requires two layers of foreach for output:

<? Php/** Author: qianshou * Date: 2014/4/28 * Comment: I was going to go to study, and the plan was ruined */$ mysql = new SaeMysql (); // This class completes database connection during initialization. // ================== =======================================$ SQL = "INSERT INTO 'demo _ table' ('id ', 'name', 'Password') VALUES ('', 'qianshou', '000000'), ('', 'naruto', 'abcde '),('', 'akaxi', 'asdfg') "; $ mysql-> runSql ($ SQL); // execute the data insertion Operation if ($ mysql-> errno ()! = 0) {die ("Error:". $ mysql-> errmsg ();} else {echo "Data inserted successfully! <Br/> ";} // ================== perform the data retrieval operation ============================= ============/// retrieve a row of Data $ SQL = "SELECT * FROM 'demo _ table' WHERE 'id' = '1 '"; $ row = $ mysql-> getLine ($ SQL); foreach ($ row as $ key => $ value) {echo $ key. "=> ". $ value. "<br/>" ;}echo "<br/> next demo <br/> "; // retrieve multiple rows of Data $ SQL = "SELECT * FROM 'demo _ table'"; $ result = $ mysql-> getData ($ SQL ); foreach ($ result as $ row) {foreach ($ row as $ key => $ value) {echo $ key. "=> ". $ value. "<br/>";} ec Ho "==============================< br/>" ;}$ mysql-> closeDb ();?>

Output result:

Id => 1
Name => qianshou
Password = & gt; 12345

Next demo
Id => 1
Name => qianshou
Password = & gt; 12345
==============================
Id => 2
Name => naruto
Password => abcde
==============================
Id => 3
Name => kakaxi
Password => asdfg
==============================

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.