PHPmysqli enhances the implementation code bitsCN.com for batch SQL statement execution
Mysqli enhancement-batch execution of SQL statements
// 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
// 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'
';
}
// Release the memory in time
$ Res-> free ();
// Determine whether a result set exists.
If ($ mysqli-> more_results ()){
Echo "********* new result set ***************
";
} Else {
Break;
}
} While ($ mysqli-> next_result ());
}
// 4. close the resource
$ Mysqli-> close ();
?>
BitsCN.com