PHP mysqli Extension Library (Object-oriented/database operations encapsulation/transaction control/precompilation), Mysqli Object-oriented _php tutorial

Source: Internet
Author: User

PHP mysqli Extension Library (Object-oriented/database operations encapsulation/transaction control/precompilation), mysqli object-oriented


1. Differences from MySQL extension libraries:

(1 higher security, more stability

(2 provides both object-oriented and process-oriented styles

2, php.ini in the Extension=php_mysqli.dll lifting the seal

3. Object-oriented: Query list

1 
 
   connect_error)   }12   #2, Operation database,   $sql = "SELECT * from User1";   $res =$ Mysqli->query ($sql);   #3, processing result (   $row = $res->fetch_row ())       Row as $key + $val)       {echo "           -$val";       }25       echo "
" }27 #4, close resources $res->free ();//Release Memory $mysqli->close ();//close connection ( ?>

4, Object-oriented: encapsulation class after implementation

4.1 Sqliconnect.class.php

1
 Mysqli=new mysqli (self:: $host, Self:: $root, Self:: $password, Self:: $DB); if (! $this->mysqli) 15 {die ("Database connection failed! ". $this->mysqli->connect_error);}18 $this->mysqli->query (" Set Nam               Es UTF8 "); 20}21 22//query operation: Public function EXCUTE_DQL ($sql) 24 {25               $res = $this->mysqli->query ($sql) or Die ("Data query Failed". $this->mysqli->error); return $res; 27 28}29 30//delete and change operation to public function Excute_dml ($sql) ($re               s= $this->mysqli->query ($sql) or Die ("Data operation failed". $this->mysqli->error); if (! $res) 35 {echo "Data operation failed"; PNs}38 else39 {($th is->mysqli->affected_rows>0) @ "echo" Operation succeeded!       "; 43             }44 else45 {echo "0 rows of data affected! ";}48}49}50 Wuyi}52?>

4.2 Calling page startsqli.php

1 
 
   excute_dml ($sql);   $sql = "select name from User1;";   $res = $Sqliconnect->excute_dql ($sql), while   ($row =$)   $res->free ();?>

5. Execute multiple database statements at the same time multiquery.php

1
 Connect_error)}12 #2, operation database, $sqls = "select * from User1;"; $sqls. = "SELECT * from User1"; #3, results of treatment if ($res = $mysqli->multi_query ($sqls)) {echo]           211 "; 24 = 25//The first result set is fetched sequentially from mysqli $result = $mysqli->store_result (); 27 28 Display Mysqli Result object ($row = $result->fetch_row ()) ($row as $ key=> $val) +}35 echo "--$val";
";}37 $result->free ();//Release the current result set in time and go to the next result set 39 40//Determine if there is a next result set 41 if (! $mysqli->more_results ()) break;44}45 echo "
New result set ************** ";}while ($mysqli->next_result ());}49 #4, close resources $mysqli-&gt ; Close ();//Closed Connection?>

6. Transaction control

1
 Connect_error); 12}13//Set the submission to False14 $mysqli->autocommit (false); $sql 1= "Update account set Balance=balan Ce+1 where id=1; /Yes Statement $sql 2= "update Accounterror2 set balance=balance-1 where id=2";//Error statement $res 1= $mysqli->query ($sql 1); 2 0 $res 2= $mysqli->query ($sql 2), if (! $res 1| |! $res 2) 23 {24//rollback: One unsuccessful rollback does not submit echo "error, rollback, please resubmit!" "; $mysqli->rollback ();//die (" Operation failed! ". $mysqli->error);}28 else29 {30//All success is submitted to echo" All submitted successfully! $mysqli->commit (),}34 $mysqli->close (), 36/* 37 1, start transaction;    Open Transaction 38 2, Svaepoint A; Do save Point 39 3, perform operation 1; 40 4, Svaepoint b;41 5, perform operation 2;42 ... 43 6, rollback to A/b; Rollback or commit 44 7, commit 45 46 transaction Control Characteristics acid atomicity/consistency/isolation/durability */48?>

7. Pretreatment Technology

Mainly in the connection and compilation process is streamlined, but also SQL to prevent injection

7.1 Precompilation Inserts multiple data

1
 Connect_error);}11 #2, creating precompiled objects $sql = "INSERT INTO User1 (name,password,email,age) values (?,?,?,?); /temporarily unassigned, use a question mark instead of $stmt = $mysqli->prepare ($sql) or Die ($mysqli->error); 15 16/******************************** The code required for repeatable execution is START*********************************/17 #3, binding parameters $name = ' xiaoming 5 '; $password = ' 34f '; $email = ' ssd@qq.co M '; $age = ' 1 '; #4, Parameter assignment (the first parameter refers to the type abbreviation for the parameter, string-s,int-i,double-d,bool-b24 $stmt->bind_param ("SSSI", $name, $pa ssWOrd, $email, $age); #5, execution code (return boolean type) $flag = $stmt->execute (); 28 29/******************************** Repeatable The code required for the line End************************************/30 #6, results, and release the "if (! $flag)" ("Operation failed"). $stmt-&gt (error);}37 Else38 {The "echo" Operation was successful! ";}41 $mysqli->close ();?>

7.2 precompiling queries for multiple data

1
 Connect_error); 10}11 12/******************************** code required for repeatable execution START*******************************/13 14 #2, create precompiled objects $sql = "Select Id,name,email from User1 where id>?;"; /id,name,email and subsequent result sets Bind_result () correspond to $stmt = $mysqli->prepare ($sql) or Die ($mysqli->error), and the binding parameter is #3 D=5;20 #4, Parameter assignment (the first parameter refers to the type abbreviation of the parameter, string-s,int-i,double-d,bool-b22 $stmt->bind_param ("i", $id);//binding parameter is $stmt-&GT ; Bind_result ($id, $name, $email);//bind result set #5, execute code (return Boolean type), $stmt->execute (), #6, remove the result set to show the while ($st Mt->fetch ()) @ echo
$id--$name--$email "; 32}33 34/******************************** code required for repeatable execution END*******************************/35 3 6 #7, results and release 37 38//Release results $stmt->free_result (); 40//Turn off precompiled statements $stmt->close (); 42//Close database connection $mysqli- >close ();?>

8. Other functions

(1 Gets the number of rows and columns Num_rows Field_count

(2 Gets a column of the result set: Header for example

$result = $mysqli->query ();

$result->fetch_field ();

(3 Remove Data

$row = $result->fetch_row (); Get each row of data

Each data is then fetched through the foreach ($row as $val) {}

http://www.bkjia.com/PHPjc/1133533.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133533.html techarticle PHP mysqli Extension Library (Object-oriented/database operations encapsulation/transaction control/precompilation), Mysqli object 1, and MySQL extension library differences: (1 security, more 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.