MYSQLI Enhancements-Batch execution SQL statements
Copy Code code as follows:
<?php
MYSQLI Enhancements-Batch execution SQL statements
//Bulk Execution DQL
//using mysqli mysqli::multi_query () Add 3 users at once
$mysqli =new mysqli ("localhost", "root", "root", "test");
if ($mysqli->connect_error) {
die ("Connection failed". $ MYSQLI->CONNECT_ERROR);
}
//Note semicolon
$sqls = INSERT INTO User1 (name,password,email,age) VALUES (' AAA ', MD5 (' AAA '), ' AAA@hu.com ', 25);
$sqls. = "INSERT into User1 (name,password,email,age) VALUES (' BBB ', MD5 (' BBB '), ' BBB@hu.com ', 25) ;";
$sqls. = "INSERT into User1 (name,password,email,age) VALUES (' CCC ', MD5 (' CCC '), ' CCC@hu.com ', 25) ;";
//Bulk execution DML can be mixed with delete insert update it is best not to use SELECT
//$SQLS. = "Update User1 set age=15 where id=1; ";
//$sqls. = "Delete from User1 where id=10";
$res = $mysqli->multi_query ($SQLS);
if (! $res) {
Echo "Operation failed". $mysqli->error;
}else{
echo "OK";
}
?>
2. Batch Inquiry
Copy Code code as follows:
<?php
The contents of the bodies and tables that use the Mysqli mysqli::multi_query () to query the table at a one-time
1. Create Mysqli objects
$mysqli =new mysqli ("localhost", "root", "root", "test");
if ($mysqli->connect_error) {
Die ("Connection failed". $mysqli->connect_error);
}
2. Batch query statement
$sqls = "Select *from user1;";
$sqls. = "desc user1";
3, processing results
If successful, there is at least one result set
if ($res = $mysqli->multi_query ($sqls)) {
do{
Remove the first result set
$res = $mysqli->store_result ();
while ($row = $res->fetch_row ()) {
foreach ($row as $val) {
Echo '--'. $val;
}
Echo ' <br/> ';
}
Free Memory in time
$res->free ();
Determine if there is a result set
if ($mysqli->more_results ()) {
echo "******** A new result set ***************<br/>";
}else{
Break
}
}while ($mysqli->next_result ());
}
4. Close Resources
$mysqli->close ();
?>