Multi_query () method in mysqli object

Source: Internet
Author: User
: This article mainly introduces the multi_query () method in the mysqli object. if you are interested in the PHP Tutorial, refer to it. Execute multiple SQL commands at a time and use the multi_query () method in the mysqli object.

Specific practices:

Write multiple SQL commands in the same string and pass them as parameters to the multi_query () method. Separate multiple sqls with semicolons. If no error occurs during the execution of the first command, TRUE is returned for this method. otherwise, FALSE is returned.

Because the multi_query () method can execute one or more queries, and each SQL command may return one result, each result set needs to be obtained when necessary. Therefore, the processing of the returned results of this method also changes. The result of the first query command must be read using the use_result () or store_result () method in the mysqli object. of course, store_result () is used () the method returns all results to the client immediately, which is more efficient. In addition, you can use the more_results () method in the mysqli object to check whether there are other result sets.

To process the next result set, call next_results () in the mysqli object to obtain the next result set. This method returns TRUE or FALSE.

If there is a result set, you also need to use the use_result () or store_result () method to read it.


The code for executing multiple SQL commands is as follows:
$ Mysqli = new mysqli ("localhost", "mysql_user", "mysql_pwd", "my_db_name ");
/* Check connection */
If ($ mysqli-> mysql_connect_errno ()){
Printf ("connection failed: % s
", Mysql_connect_error ());
Exit ();
}
$ Query = "set names GB2312 ;";
$ Query. = "SELECT CURERENT_USER ();" // get the user name using the connection
$ Query. = "SELECT name, phone FROM contactinfo LIMIT 2 ";
/* Execute multi query */
If ($ mysqli-> multi_query ($ query )){
Do {
If ($ result = $ mysqli-> store_result ()){
// $ Mysqli-> store_result () get the first result set
While ($ row = $ result-> fetch_row ()){
Foreach ($ row as $ data ){
Echo $ data ."";
}
}
Echo'
';
}

If ($ mysqli-> more_results ()){
Echo "----------------------------
";
}
} While ($ mysqli-> next_result ());
}
/* Close connection */
$ Mysqli-> close ();
?>

Note:

In the preceding example, use the multi_query () method in the mysqli object to execute three SQL commands at a time to obtain multiple result sets and traverse data from them. If an error occurs during the command processing, the multi_query () and next_result () methods may fail.

The returned values of the multi_query () method and the attributes errno, error, and info of mysqli are only related to the first SQL command. it is impossible to determine whether an error occurs during execution of the second and later commands. Therefore, when the returned value of the multi_query () method is TRUE, it does not mean that there is no error in subsequent command execution.

The above describes the multi_query () method in the mysqli object, including some content, and hope to help those who are interested in the PHP Tutorial.

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.