SoFunction
Updated on 2025-03-09

Example of PHP using mysqli to execute multiple SQL query statements at the same time

In PHP database operation, mysqli has great advantages over mysql, and it is recommended that you use it; we have introduced it before.How to operate database using mysqli's prepare in PHP5, using mysqli supports multi-query features, please see the following php code:

<?php 
$mysqli = new mysqli("localhost","root","","123456");
$mysqli->query("set names 'utf8'");
//Multiple SQL statements$sql = "select id,name from `user`;";
$sql .= "select id,mail from `user`";
if ($mysqli->multi_query($sql)){// Use multi_query() to execute one or more SQL statements do{
 if ($rs = $mysqli->store_result()){//store_result() method gets the first SQL statement query result  while ($row=$rs->fetch_row()){
  var_dump($row);
  echo "<br>";
  }
  $rs->Close(); //Close the result set  if ($mysqli->more_results()){ //Judge if there are more result sets  echo "<hr>";
  }
 }
 }while($mysqli->next_result()); //next_result() method gets the next result set and returns the bool value}
$mysqli->close(); //Close the database connection?>

Through the above example, I believe everyone can easily understand that when using it, you should pay special attention to itmulti_query()When executing multiple statements, the statements are separated by ;, otherwise an error will occur.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links