Home >Backend Development >PHP Tutorial >Share an example of how PHP implements mysqli batch execution of multiple statements

Share an example of how PHP implements mysqli batch execution of multiple statements

黄舟
黄舟Original
2017-07-22 15:18:291715browse

This article mainly introduces the method of PHP to implement mysqli to execute multiple statements in batches. It analyzes the related operation skills of PHP to connect to mysqli and execute multiple statements in batches in the form of examples. Friends in need can refer to the following

The example in this article describes how PHP implements mysqli to execute multiple statements in batches. Share it with everyone for your reference, the details are as follows:

You can perform multiple operations or retrieve multiple result sets at one time.

Example:


<?php
$mysqli = new mysqli("localhost", "root", "111111", "test");
/* check connection */
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}
/* multi_query执行一个或多个针对数据库的查询。多个查询用分号进行分隔。 */
$query = "SELECT * from test where id = 1;";
$query .= "SELECT name FROM test";
/* 批量执行查询 ,如果第一个查询失败则返回 FALSE。*/
if ($mysqli->multi_query($query)) {
  do {
    /* 获取第一个结果集 */
    if ($result = $mysqli->store_result()) {
      while ($row = $result->fetch_row()) {
        printf("%s\n", $row[0]);
      }
      $result->free();
    }
    /* 检查一个多查询是否有更多的结果 */
    if ($mysqli->more_results()) {
      printf("-----------------\n");
    }
    //准备下一个结果集
  } while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
?>

The above is the detailed content of Share an example of how PHP implements mysqli batch execution of multiple statements. For more information, please follow other related articles on the PHP Chinese website!

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