PHPmysqli Extension Library (object-oriented/database operation encapsulation/transaction control/pre-compilation), mysqli object-oriented _ PHP Tutorial

Source: Internet
Author: User
PHPmysqli Extension Library (object-oriented database operation encapsulation transaction control pre-compilation), and mysqli object-oriented. PHPmysqli Extension Library (object-oriented database operation encapsulation transaction control pre-compilation ), mysqli object-oriented 1. differences with mysql extension libraries: (1. higher security and stability (2. PHP mysqli Extension Library (object-oriented/database operation encapsulation/transaction control/pre-compilation ), mysqli object-oriented

1. Differences from mysql extension libraries:

(1. high security and stability

(2) provides two styles: Object-Oriented and process-oriented.

2. in php. ini, extend = php_mysqli.dll to unseal

3. object-oriented: query list

1
 Connect_error); 11} 12 13 #2. Operate Database 14 15 $ SQL = "select * from user1"; 16 $ res = $ mysqli-> query ($ SQL ); 17 #3. processing result 18 19 while ($ row = $ res-> fetch_row () 20 {21 foreach ($ row as $ key => $ val) 22 {23 echo "-- $ val"; 24} 25 echo"
"; 26} 27 #4. close resource 28 $ res-> free (); // release memory 29 $ mysqli-> close (); // close connection 30 31?>

4. object-oriented: implementation after encapsulation class

4.1 Sqliconnect. class. php

1
 Mysqli = new MySQLi (self ::$ host, self ::$ root, self ::$ password, self ::$ db); 14 if (! $ This-> mysqli) 15 {16 die ("database connection failed! ". $ This-> mysqli-> connect_error); 17} 18 19 $ this-> mysqli-> query ("set names utf8 "); 20} 21 22 // query operation 23 public function excute_dql ($ SQL) 24 {25 $ res = $ this-> mysqli-> query ($ SQL) or die ("Data Query failed ". $ this-> mysqli-> error); 26 return $ res; 27 28} 29 30 // add, delete, modify, and delete 31 public function excute_dml ($ SQL) 32 {33 $ res = $ this-> mysqli-> query ($ SQL) or die ("data operation failed ". $ this-> mysqli-> error); 34 if (! $ Res) 35 {36 echo "data operation failed"; 37} 38 else39 {40 if ($ this-> mysqli-> affected_rows> 0) 41 {42 echo "operation successful! "; 43} 44 else45 {46 echo" 0 rows of data are affected! "; 47} 48} 49} 50 51} 52?>

4.2 Call page startsqli. php

1
 Excute_dml ($ SQL); 13 14 $ SQL = "select name from user1;"; 15 $ res = $ Sqliconnect-> excute_dql ($ SQL ); 16 while ($ row = $) 17 18 $ res-> free (); 19?>

5. execute multiple database statements multiQuery. php at the same time

1
 Connect_error); 11} 12 13 #2. Operate Database 14 15 $ sqls = "select * from user1;"; 16 $ sqls. = "select * from user1"; 17 18 #3. processing result 19 20 if ($ res = $ mysqli-> multi_query ($ sqls) 21 {22 echo "211 "; 23 do 24 {25 // extract the first result set from mysqli continuously 26 $ result = $ mysqli-> store_result (); 27 28 // Display mysqli result object 29 while ($ row = $ result-> fetch_row () 30 {31 foreach ($ row as $ key => $ val) 32 {33 echo "-- $ val"; 34} 35 echo"
"; 36} 37 38 $ result-> free (); // release the current result set in time, and enter the next result set 39 40 // determine whether there is a next result set 41 if (! $ Mysqli-> more_results () 42 {43 break; 44} 45 echo"
**************"; 46 47} while ($ mysqli-> next_result (); 48} 49 50 #4. close resource 51 $ mysqli-> close (); // close the connection 52 53 54?>

6. transaction control

1
 Connect_error); 12} 13 // Set the submission to false14 $ mysqli-> autocommit (false ); 15 16 $ sql1 = "update account set balance = balance + 1 where id = 1 ;"; // correct statement 17 $ sql2 = "update accounterror2 set balance = balance-1 where id = 2 "; // Wrong statement 18 19 $ res1 = $ mysqli-> query ($ sql1); 20 $ res2 = $ mysqli-> query ($ sql2); 21 22 if (! $ Res1 |! $ Res2) 23 {24 // rollback: if one of them is unsuccessful, rollback will not be submitted. 25 echo "incorrect. Rollback. please submit it again! "; 26 $ mysqli-> rollback (); // die (" Operation failed! ". $ Mysqli-> error); 27} 28 else29 {30 // if all requests are successful, 31 echo is submitted." All requests are submitted successfully! "; 32 $ mysqli-> commit (); 33} 34 35 $ mysqli-> close (); 36/* 37 1, start transaction; start transaction 38 2, svaepoint; save point 39 3. execute Operation 1; 40 4. svaepoint B; 41 5. execute Operation 2; 42... 43 6. rollback to a/B; rollback or commit 44 7. commit 45 46 transaction control features acid atomicity/consistency/isolation/persistence 47 */48?>

7. preprocessing technology

The connection and compilation process is simplified, and SQL can prevent injection.

7.1 Pre-compile and insert multiple data

1
 Connect_error); 10} 11 12 #2. create a pre-compiled object 13 $ SQL = "insert into user1 (name, password, email, age) values (?,?,?,?); "; // Do not assign values for the moment. Use a question mark instead of 14 $ stmt = $ mysqli-> prepare ($ SQL) or die ($ mysqli-> error ); 15 16 /* code start *********************************/17 #3, binding parameter 18 $ name = 'xiaoming 5 '; 19 $ password = '34f'; 20 $ email = 'ssd @ qq.com '; 21 $ age = '1 '; 22 23 #4. parameter assignment (the first parameter refers to the abbreviated type of the parameter, string-s, int-I, double-d, bool-b24 $ stmt-> bind_param ("sssi ", $ name, $ password, $ email, $ age); 25 26 #5. run the code (Return Boolean type) 27 $ flag = $ stmt-> execute (); 28 29/********************************* repeated execution is required code end ************************************/ 30 31 #6. result and release 32 33 if (! $ Flag) 34 {35 die ("Operation failed". $ stmt-> error); 36} 37 else38 {39 echo "operation successful! "; 40} 41 42 $ mysqli-> close (); 43 44 45?>

7.2 Pre-compile and query multiple data items

1
 Connect_error ); 10} 11 12/******************************** repeated execution required code start ********************************/13 14 #2. create a pre-compiled object 15 $ SQL = "select id, name, email from user1 where id> ?; "; // Id, name, email, and bind_result () of the subsequent result set correspond to 16 $ stmt = $ mysqli-> prepare ($ SQL) or die ($ mysqli-> error); 17 18 #3. bind the parameter 19 $ id = 5; 20 21 #4. assign a value to the parameter (the first parameter refers to the abbreviation of the parameter type, string-s, int-I, double-d, bool-b22 $ stmt-> bind_param ("I", $ id ); // bind_result ($ id, $ name, $ email) binding parameter 23 $ stmt-> bind_result ($ id, $ name, $ email); // bind result set 24 25 #5. execute code (Return Boolean type) 26 $ stmt-> execute (); 27 28 #6. 29 while ($ stmt-> fetch () 30 {31 echo"
$ Id -- $ name -- $ email "; 32} 33 34/******************************** repeated execution code end ********************************/35 36 #7. result and release 37 38 // release result 39 $ stmt-> free_result (); 40 // close the pre-compiled statement 41 $ stmt-> close (); 42 // close the database connection 43 $ mysqli-> close (); 44 45 46?>

8. Other functions

(1) obtain the number of rows and columns num_rows field_count

(2) obtain a column of the result set: the header is

$ Result = $ mysqli-> query ();

$ Result-> fetch_field ();

(3. retrieve data

$ Row = $ result-> fetch_row (); // Obtain each row of data

Then retrieve each data through foreach ($ row as $ val) {}

Http://www.bkjia.com/PHPjc/1133533.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133533.htmlTechArticlePHP mysqli Extension Library (object-oriented/database operation encapsulation/transaction control/pre-compiled), mysqli object-oriented 1, and mysql Extension Library difference: (1. higher security and stability (2 provides...

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.