PHP mysqli Bulk Execute SQL Statement program code _php Tutorial

Source: Internet
Author: User
This article to introduce to you the PHP mysqli bulk Execution of SQL statement program code, there is a need to know friends can refer to.

Mysqli enhanced-Bulk execution of SQL statements

The code is as follows Copy Code


Mysqli enhanced-Bulk execution of SQL statements

Batch Execution DQL
Add 3 users at a time using Mysqli's 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);";

Batch execution DML can be mixed 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

code as follows copy code

Use Mysqli's mysqli::multi_query () to query the table for the contents of the Organization and table

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 results
If successful, there is at least one result set
if ($res = $mysqli->multi_query ($sqls)) {

do{
Take out the first set of results
$res = $mysqli->store_result ();
while ($row = $res->fetch_row ()) {
foreach ($row as $val) {
Echo '--'. $val;
}
Echo '
';
}

Free up memory in time
$res->free ();
Determine if there is a result set
if ($mysqli->more_results ()) {
echo "******** new result set ***************
";
}else{
Break
}

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

}
4. Close Resources
$mysqli->close ();
?>

http://www.bkjia.com/PHPjc/630720.html www.bkjia.com true http://www.bkjia.com/PHPjc/630720.html techarticle this article to introduce to you the PHP mysqli bulk Execution of SQL statement program code, there is a need to know friends can refer to. MYSQLI enhanced-Bulk Execute SQL statement code following copy code ...

  • 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.