跳转到主要内容

multi-query

mysqli::multi_query

mysqli_multi_query

Performs one or more queries on the database

ENTITY

ENTITY

public

bool

mysqli::multi_query

string

query

ENTITY

bool

mysqli_multi_query

mysqli

mysql

string

query

Executes one or multiple queries which are concatenated by a semicolon.

ENTITY

Queries are sent in a single call to the database and processed sequentially. mysqli_multi_query waits for the first query to complete before returning control to PHP. In the meantime, the MySQL server will continue processing the remaining queries asynchronously from PHP and make the results available for fetching.

For queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, mysqli_use_result or mysqli_store_result can be used to retrieve the result set. For queries which do not produce a result set, the same functions can be used to retrieve information such as the number of affected rows.

[!TIP] Executing CALL statements for stored procedures can produce multiple result sets. If the stored procedure contains SELECT statements, the result sets are returned in the order that they are produced as the procedure executes. In general, the caller cannot know how many result sets a procedure will return and must be prepared to retrieve multiple results. The final result from the procedure is a status result that includes no result set. The status indicates whether the procedure succeeded or an error occurred.

ENTITY

query
A string containing the queries to be executed. Multiple queries must be separated by a semicolon.

ENTITY

Returns ENTITY if the first statement failed. To retrieve subsequent errors from other statements you have to call mysqli_next_result first.

ENTITY ENTITY

ENTITY

mysqli::multi_query example

ENTITY

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
$mysqli->multi_query($query);
do {
    /* store the result set in PHP */
    if ($result = $mysqli->store_result()) {
        while ($row = $result->fetch_row()) {
            printf("%s\n", $row[0]);
        }
    }
    /* print divider */
    if ($mysqli->more_results()) {
        printf("-----------------\n");
    }
} while ($mysqli->next_result());

   

ENTITY

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
mysqli_multi_query($link, $query);
do {
    /* store the result set in PHP */
    if ($result = mysqli_store_result($link)) {
        while ($row = mysqli_fetch_row($result)) {
            printf("%s\n", $row[0]);
        }
    }
    /* print divider */
    if (mysqli_more_results($link)) {
        printf("-----------------\n");
    }
} while (mysqli_next_result($link));

   

ENTITY

my_user@localhost
-----------------
Amersfoort
Maastricht
Dordrecht
Leiden
Haarlemmermeer

ENTITY

mysqli_query, mysqli_use_result, mysqli_store_result, mysqli_next_result, mysqli_more_results