In PHP, mysqli_multi_query() sends one or more SQL statements separated by semicolons in a single call and returns true if the first statement was accepted.
After a successful call you must walk every result with mysqli_more_results(), mysqli_next_result() and store_result()/use_result(). For everyday single statements, prefer mysqli_query() or prepared statements.
Syntax
Call mysqli_multi_query() with the values you need. Start with this line.
if (mysqli_multi_query($mysqli, $sql)) { /* drain results */ }
Checks the first statement, then processes the rest.
Multi-query is powerful and easy to misuse. Never concatenate untrusted user input into a multi-statement string. Prefer mysqli_prepare() / mysqli_query() for normal app SQL. Stored procedures that return multiple sets also need the same drain loop.
Required. One or more SQL statements separated by semicolons.
Return Value
Return Value
Description
bool
true if the first statement executed without error; false on failure for that first statement. Later statements need the next_result loop to surface their errors.
PHP Examples
Example 1 — Run Two SELECTs as a Batch
Send two statements and count how many result sets appear. Sample counts are fixed.
PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_multi_query($mysqli, "SELECT 'a' AS v; SELECT 'b' AS v;");
$count = 0;
do {
if ($result = mysqli_store_result($mysqli)) {
$count++;
mysqli_free_result($result);
}
} while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli));
echo "Sets: ", $count, PHP_EOL;
mysqli_close($mysqli);
Output
Sets: 2
How It Works
multi_query sends both SELECTs together.
store_result reads each result set in turn.
more_results + next_result advance through the batch.
We counted two sets in this sample.
Example 2 — Mix INSERT and SELECT
Some statements return no result object; still advance with next_result.
PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
$sql = "INSERT INTO logs (msg) VALUES ('hi'); SELECT COUNT(*) AS c FROM logs;";
mysqli_multi_query($mysqli, $sql);
do {
if ($result = mysqli_store_result($mysqli)) {
$row = mysqli_fetch_assoc($result);
echo "count=", $row["c"], PHP_EOL;
mysqli_free_result($result);
}
} while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli));
mysqli_close($mysqli);
Output
count=1
How It Works
INSERT produces no mysqli_result, so store_result may return false.
The loop still calls next_result for the following SELECT.
The SELECT set is fetched and printed.
Skipping the loop would leave the connection out of sync.
Example 3 — Prefer Single Queries for App Code
Show the simpler mysqli_query path most beginners should use.