Mysqli multi-query feature for querying Multiple SQL statements

Source: Internet
Author: User
Tags mysql tutorial

Mysqli has many advantages over mysql. We recommend that you use it. If you do not know it, you can view the basic mysql Tutorial:

Use mysqli to connect to the database and use mysqli preprocessing prepare. In addition, mysqli supports the multi-query feature. refer to the following php code:
Copy codeThe Code is as follows:
<? Php
$ Mysqli = new mysqli ("localhost", "root", "", "new ");
$ Mysqli-> query ("set names 'utf8 ");
// Multiple SQL statements
$ SQL = "select id, name from 'user ';";
$ SQL. = "select id, mail from 'user '";
Echo $ SQL;
If ($ mysqli-> multi_query ($ SQL) {// multi_query () executes one or more SQL statements
Do {
If ($ rs = $ mysqli-> store_result () {// store_result () method to obtain the query result of the first SQL statement
While ($ row = $ rs-> fetch_row ()){
Var_dump ($ row );
Echo "<br> ";
}
$ Rs-> Close (); // Close the result set.
If ($ mysqli-> more_results () {// determine whether there are more result sets
Echo "}
}
} While ($ mysqli-> next_result (); // obtain the next result set using the next_result () method, and return the bool value.
}
$ Mysqli-> close (); // close the database connection
?>


I have already commented out some of the methods used. Note that when multi_query () executes multiple statements, they are separated by commas (,). Otherwise, errors may occur.

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.