PHP mysqli enhanced implementation code for batch SQL statement execution

Source: Internet
Author: User

Mysqli enhancement-Batch execution of SQL statements

CopyCode The 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 ();
?>

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.