Mysqli enhancement-Batch execution of SQL statements
Copy codeThe Code is as follows:
<? Php
// Mysqli enhancement-Batch execution of SQL statements
// Execute dql in batches
// Use mysqli: multi_query () of mysqli to add three users at a time
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ("connection failed". $ 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 of dml can be performed in combination 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 query
Copy codeThe Code is as follows:
<? Php
// Use mysqli: multi_query () of mysqli to query the structure and content of a table at a time.
// 1. Create a mysqli object
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ("connection failed". $ mysqli-> connect_error );
}
// 2. batch query statements
$ Sqls = "select * from user1 ;";
$ Sqls. = "desc user1 ";
// 3. processing result
// If successful, there must be at least one result set
If ($ res = $ mysqli-> multi_query ($ sqls )){
Do {
// Retrieve the first result set
$ Res = $ mysqli-> store_result ();
While ($ row = $ res-> fetch_row ()){
Foreach ($ row as $ val ){
Echo '--'. $ val;
}
Echo '<br/> ';
}
// Release the memory in time
$ Res-> free ();
// Determine whether a result set exists.
If ($ mysqli-> more_results ()){
Echo "************************ <br/> ";
} Else {
Break;
}
} While ($ mysqli-> next_result ());
}
// 4. Close the resource
$ Mysqli-> close ();
?>