PHP mysqli enhanced implementation code for bulk execution of sql statements
- 2020-05-15 02:25:44
- OfStack
mysqli enhancement - batch execution of sql statements
<?php
//mysqli To enhance - Batch execution sql statements
// Batch execution dql
// use mysqli the mysqli::multi_query() 1 Time sex add 3 A user
$mysqli =new MySQLi("localhost","root","root","test");
if($mysqli->connect_error){
die (" The connection fails ".$mysqli->connect_error);
}
// Note the 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);";
// Batch execution dml Can be mixed delete insert update It's 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 " The operation failure ".$mysqli->error;
}else{
echo "OK";
}
?>
2. Batch query
<?php
// use mysqli the mysqli::multi_query() 1 Subquery the organization of the table and the contents of the table
//1 , create, mysqli object
$mysqli=new MySQLi("localhost","root","root","test");
if($mysqli->connect_error){
die(" The connection fails ".$mysqli->connect_error);
}
//2 , batch query statement
$sqls="select *from user1;";
$sqls.="desc user1";
//3 , processing results
// If it works, at least it does 1 A result set
if($res=$mysqli->multi_query($sqls)){
do{
// Take the first 1 A 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 are any more result sets
if($mysqli->more_results()){
echo "******** New result set ***************<br/>";
}else{
break;
}
}while($mysqli->next_result());
}
//4 , close the resource
$mysqli->close();
?>