PHP uses mysqli to execute multiple instances of sql query statements simultaneously

  • 2021-12-04 18:21:29
  • OfStack

In PHP database operation, mysqli has great advantages over mysql, so it is recommended to use it; Before, we have introduced how to use prepare of mysqli to operate the database in PHP5, and use mysqli to support multi-query features. Please see the following php code:


<?php 
$mysqli = new mysqli("localhost","root","","123456");
$mysqli->query("set names 'utf8'");
// Multiple sql Statement 
$sql = "select id,name from `user`;";
$sql .= "select id,mail from `user`";
if ($mysqli->multi_query($sql)){// Use multi_query() Execute 1 Strip or strips sql Statement 
 do{
 if ($rs = $mysqli->store_result()){//store_result() Method to get the first 1 Article sql Statement query results 
  while ($row=$rs->fetch_row()){
  var_dump($row);
  echo "<br>";
  }
  $rs->Close(); // Close the result set 
  if ($mysqli->more_results()){ // Determine if there are more result sets 
  echo "<hr>";
  }
 }
 }while($mysqli->next_result()); //next_result() Method to get the following 1 Result set, returning bool Value 
}
$mysqli->close(); // Close the database connection 
?>

Through the above example, I believe everyone can easily understand that when using it, special attention should be paid to multi_query() When executing multiple statements, the statements are used; Separated, otherwise errors will occur.

Summarize


Related articles: