PHP will manipulate the database, encapsulate it as a tool class, and learn to program in an object-oriented way.

Source: Internet
Author: User

<?PHPclassSqlTool {//Properties        Private$conn; Private$host ="localhost"; Private$user ="Root"; Private$password ="Root"; Private$db ="Test"; function SqlTool () {$ This->conn=mysql_connect ($ This->host,$ This->user,$ This-password); if(!$ This-conn) {Die ("failed to connect to database". Mysql_error ()); } mysql_select_db ($ This->db,$ This-conn); mysql_query ("Set names UTF8");//Set character sets        }        //Method: //Complete Select Dql         Publicfunction Execute_dql ($sql) {$res=mysql_query ($sql, $ This-conn) or Die (Mysql_error ()); return$res; }        //complete update,delete, insert DML         Publicfunction Execute_dml ($sql) {$b=mysql_query ($sql, $ This-conn); //echo "added id=". mysql_insert_id ($this->conn);            if(!$b) {                return 0;//failed}Else{                if(Mysql_affected_rows ($ This->conn) >0){                    return 1;//indicates success}Else{                    return 2;//indicates no row count effect.                }            }        }    }?>

2. Use php 's mysqli Extension Library to operate the MySQL database

Brief introduction :

MYSQLI ( enhanced version of MySQL improve MySQL extension library )

Comparison of MySQL extension library and mysqli Extension Library

    1. Mysqli stability and safety, increased efficiency
    2. MYSQI supports object-oriented programming while mysqli extension libraries take into account the old php Programmer, providing a process-oriented programming style .

Mysqli has two sets of programming styles :

$mysqli =new  mysqli ("localhost", "root", "roo3t", "Test"); // in an object-oriented manner if ($mysqli-connect_error) {die    ($mysqli-connect_error);} // consider a compatible low version if (Mysqli_connect_error ()) {die    ("Connection Error". Mysqli_connect_error ())}

Quick Start for mysqli programming

Write a program that reads data from the User1 table and prints it in a Web page. ( use mysqli to complete .)

    1. Use the mysqli Object-oriented style to complete the case first

1.1. Configure the php.ini file to allow PHP to support the mysqli Extension Library

Extension=php_mysqli.dll

1.2 build a library, build a table .

Here we use the original user1 table .

1.3 Writing code

<?PHP Header ("Content-type:text/html;charset=utf-8"); //mysqli working with MySQL database (object oriented style)//1. Create a Mysqli object$mysqli =NewMysqli ("localhost","Root","Root","Test"); //Verify if OK    if($mysqliconnect_error) {Die ("Connection Failed". $mysqliconnect_error); }    //2. Operational database (send SQL)$sql ="select * from User1"; //$res is the result set. Mysqli Result$res = $mysqliquery ($sql); //Var_dump ($res); //3. Processing results mysql_fetch_row ();     while($row = $resFetch_row ()) {        foreach($row as$key =$val) {echo"--$val"; } Echo"<br/>"; }    //4. Close Resources//Freeing Memory$resFree (); //Close Connection$mysqliclose ();?>

3. Then use the process-oriented approach to show you .

//1. Get mysqli connectionHeader"Content-type:text/html;charset=utf-8"); $mysqli=mysqli_connect ("localhost","Root","Root","Test"); if(!$mysqli) {Die ("Connection Failed". Mysqli_connnect_error ($mysqli)); }    //2. Send SQL statements to the database (Ddl,dml dql ...)$sql ="select * from User1"; $res=mysqli_query ($mysqli, $sql); //Var_dump ($res); //3. Processing the resulting results//loop out the data in the $res mysqli_fetch_row mysql_fetch_row     while($row =Mysqli_fetch_row ($res)) {                foreach($row as$key =$val) {echo"--$val"; } Echo"<br/>"; }    //4. Close ResourcesMysqli_free_result ($res); Mysqli_close ($mysqli);

? In the mysqli Extension, there are also four ways to get the mysqli result set

MYSQLI_RESULT::FETCH_ASSOC <==> Mysql_fetch_assoc

Mysqli_result::fetch_row <==> Mysql_fetch_row

Mysqli_result::fetch_array <===> Mysql_fetch_array

Mysqli_result::fetch_object<===> Mysql_fetch_object

Here we recommend that you use the first two high efficiency

? There are three ways to release a result set in mysqli :

void Mysqli_result::free (void)

void Mysqli_result::close (void)

void Mysqli_result::free_result (void)

? a special description of the MySQL SQL statement :

If the field type of the operation is string , then we are required to include.

If the field type of the operation is numeric, it can be included with ' 80 ' or without

Enhanced U mysqli - execute SQL statements in bulk

Batch execution of DML statements

Basic syntax

$sqls = "SQL1;SQL2; ..."
Mysqli::multi_query ($SQLS)

please use mysqli 's mysqi::multi_query () to add three user Songjianglu for one time

$sqls ="insert INTO User1 (name,password,email,age) VALUES (' Song Jiang ', ' aaa ', ' [email protected] ', ';"; $sqls.="insert INTO User1 (name,password,email,age) VALUES (' Lu Junyi ', ' aaa ', ' [email protected] ', $);"; $sqls.="insert INTO User1 (name,password,email,age) VALUES (' Wu ', ' aaa ', ' [email protected] ', ', ');";//$sqls. = "UPDATE;";//$sqls. = "delete;";//DML and DQL$b= $mysqli->multi_query ($SQLS);

? Bulk execution DML statements can be mixed using the delete insert Update, but it is best not to use the select

PHP will manipulate the database, encapsulate it as a tool class, and learn to program in an object-oriented way.

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.