Home > Backend Development > PHP Tutorial > How to batch execute multiple statements in PHPmysqli

How to batch execute multiple statements in PHPmysqli

墨辰丷
Release: 2023-03-26 19:14:01
Original
2496 people have browsed it

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 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();
?>
Copy after login

Related recommendations:

php mysqli batch query Methods for querying multiple tables of data using phpmysqli_PHP Tutorial

php mysqli Methods for inserting, updating and deleting data in batches, phpmysqli_PHP Tutorial

php mysqli implements batch execution of insert, update and delete data methods, phpmysqli_PHP tutorial

The above is the detailed content of How to batch execute multiple statements in PHPmysqli. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template