PHP mysqli Batch Execute SQL statement program code

Source: Internet
Author: User
Tags md5

MYSQLI Enhancements-Batch execution SQL statements

The code is as follows Copy Code


<?php

MYSQLI Enhancements-Batch execution SQL statements

Bulk execution DQL
Add 3 users at once using Mysqli mysqli::multi_query ()

$mysqli =new mysqli ("localhost", "root", "root", "test");
if ($mysqli->connect_error) {
Die ("Connection failed". $mysqli->connect_error);
}

Note 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);";

Bulk execution DML can be mixed with delete insert update best not 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 Inquiry

The code is as follows Copy Code

<?php

The contents of the bodies and tables that use the Mysqli mysqli::multi_query () to query the table at a one-time

1. Create Mysqli objects
$mysqli =new mysqli ("localhost", "root", "root", "test");
if ($mysqli->connect_error) {
Die ("Connection failed". $mysqli->connect_error);
}
2. Batch query statement
$sqls = "Select *from user1;";
$sqls. = "desc user1";
3, processing results
If successful, there is at least one result set
if ($res = $mysqli->multi_query ($sqls)) {

do{
Remove the first 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 is a result set
if ($mysqli->more_results ()) {
echo "******** A new result set ***************<br/>";
}else{
Break
}

}while ($mysqli->next_result ());

}
4. Close Resources
$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.