Phpmysqli batch execute SQL statement program code _ PHP Tutorial

Source: Internet
Author: User
Phpmysqli executes SQL statement program code in batches. This article will introduce you to PHPmysqli to execute SQL statement program code in batches. For more information, see. Mysqli enhancement-batch execution of SQL statement code: Copy the code below. This article will introduce you to PHP mysqli batch execution of SQL statement program code. For more information, see.

Mysqli enhancement-batch execution of SQL statements

The code is as follows:


// 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

The code is as follows:

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

Mysqli executes SQL statement program code in batches. For more information, see. Mysqli enhancement-code for batch execution of SQL statements is as follows...

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.