Mysqli Object-oriented practice--multi_query

Source: Internet
Author: User

you can implement multiple SQL statements using Multi_query, and each SQL statement is separated by semicolons. It is important to note that:

In a semicolon-delimited SQL statement, this SQL statement and subsequent SQL statements will not execute as long as one of the SQL statements fails to execute.

The return value of Multi_query () is false only if the first SQL statement fails to execute. If the first SQL statement executes successfully, it will return true.

<?php    $mysqli = new mysqli ();    $mysqli->connect ("localhost", "root", "root", "test");    if ($mysqli->connect_errno) {die        ($mysqli->connect_error);    }    $mysqli->set_charset ("UTF8");    $sql = "";    $sql. = "TRUNCATE table mysqli;"; The first SQL statement    $sql. = "INSERT into mysqli (ID, name) VALUES (null, ' AAAA '), (null, ' bbbb ');"; /second SQL statement    $sql. = "Update table mysqli where uid = 1;"; /Third SQL statement, because there is no UID field, so there is an error    $sql. = "INSERT into mysqli (ID, name) VALUES (null, ' CCCC ');";    $res = $mysqli->multi_query ($sql);    Var_dump ($res);? >

To view the database:

Mysql> SELECT * FROM mysqli;+----+------+| ID | Name |+----+------+|  1 | AAAA | |  2 | BBBB |+----+------+2 rows in Set (0.00 sec)

You can see that the third SQL statement failed to execute, and the fourth SQL statement that inserted the data did not execute

multi_query Executing multiple select query statements

For multiple SELECT statements, there will be multiple result sets returned, so you need to "switch result sets",

You can use Mysqli_result Mysqli::use_result and Mysqli_result Mysqli::store_result () to get the result set that the pointer points to in the results of the multi_query. Traverse multiple result sets by moving the internal pointer.

You can use bool Mysqli::more_result () to detect if there is a result set, and if so, you can point the internal pointer to the next result set through bool Mysqli::next_result ().

Method One: Use Mysqli_result mysqli::use_result ()
<?php    $mysqli = new mysqli ();    $mysqli->connect ("localhost", "root", "root", "test");    if ($mysqli->connect_errno) {die        ($mysqli->connect_error);    }    $mysqli->set_charset ("UTF8");    $sql = "";    $sql. = "SELECT * from mysqli;";    $sql. = "SELECT * from User;";    $res = $mysqli->multi_query ($sql);    if ($res) {        do{            if ($mysqli _result = $mysqli->use_result ()) {                Print_r ($mysqli _result->fetch_all (MYSQLI_ASSOC));            }        } while ($mysqli->more_results () && $mysqli->next_result ());    } else {        echo "error". $mysqli->errno. ":". $mysqli->error;    }? >

  

Method Two: Mysqli_result Mysqli::store_result ()
<?php    $mysqli = new mysqli ();    $mysqli->connect ("localhost", "root", "root", "test");    if ($mysqli->connect_errno) {die        ($mysqli->connect_error);    }    $mysqli->set_charset ("UTF8");    $sql = "";    $sql. = "SELECT * from mysqli;";    $sql. = "SELECT * from User;";    $res = $mysqli->multi_query ($sql);    if ($res) {        do{            if ($mysqli _result = $mysqli->store_result ()) {                Print_r ($mysqli _result->fetch_ All (MYSQLI_ASSOC));            }        } while ($mysqli->more_results () && $mysqli->next_result ());    } else {        echo "error". $mysqli->errno. ":". $mysqli->error;    }? >

  

Mysqli Object-oriented practice--multi_query

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.