Home  >  Article  >  Backend Development  >  multi_query() method in mysqli object

multi_query() method in mysqli object

WBOY
WBOYOriginal
2016-08-08 09:26:151153browse

To execute multiple SQL commands at one time, use the multi_query() method in the mysqli object.

Specific method:

Write multiple SQL commands in the same string and pass them as parameters to the multi_query() method. Use semicolons (;) to separate multiple SQL commands. If there are no errors in the execution of the first command, this method will return TRUE, otherwise it will return FALSE.

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

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

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


The code to execute 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());                                                                $query .="SELECT CURERENT_USER();"                                                                                                                                                                                                                                          // Get the user name using the connection
        $query.="SELECT name,phone FROM contactinfo LIMIT 2"; "                                                                                                                                                                    ->fetch_row()){
                    foreach($row as $data){
                        echo $data."  ";
                    }
                }
                echo '
';
            }
                    
            if($ mysqli-& gt; more_results ()) {
echo "---------------------------- & lt; br & gt; while($mysqli->next_result());
}
/* close connection */
$mysqli->close();
?>

Note:

In the above example, use the mysqli object The multi_query() method executes three SQL commands at once, obtaining multiple result sets and iterating over data from them. If an error occurs during command processing, problems will arise with the multi_query() and next_result() methods.

    The return value of the multi_query() method, as well as the attributes errno, error, info, etc. of mysqli are only related to the first SQL command. It is impossible to determine whether an error occurred during the execution of the second and subsequent commands. Therefore, when the return value of the multi_query() method is TRUE, it does not mean that there are no errors in the execution of subsequent commands.
The above introduces the multi_query() method in the mysqli object, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn