PHP mysqli_multi_query() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

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.

PHP
$ok = mysqli_multi_query($mysqli, $sqlBatch);
CallWhat it does
mysqli_multi_query($mysqli, "SELECT 1; SELECT 2;")Sends two statements in one batch.
mysqli_multi_query($mysqli, $sql)Runs a semicolon-separated script string.
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.

Library declarations
PHP
mysqli_multi_query(mysqli $mysql, string $query): bool

Parameters

ParameterDescription
mysqlRequired. The open mysqli connection.
queryRequired. One or more SQL statements separated by semicolons.

Return Value

Return ValueDescription
booltrue 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);

How It Works

  1. multi_query sends both SELECTs together.
  2. store_result reads each result set in turn.
  3. more_results + next_result advance through the batch.
  4. 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);

How It Works

  1. INSERT produces no mysqli_result, so store_result may return false.
  2. The loop still calls next_result for the following SELECT.
  3. The SELECT set is fetched and printed.
  4. 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.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

$result = mysqli_query($mysqli, "SELECT id, name FROM users LIMIT 3");
echo "Rows: ", mysqli_num_rows($result), PHP_EOL;
mysqli_free_result($result);

mysqli_close($mysqli);

How It Works

  1. One statement does not need multi_query.
  2. mysqli_query() returns the result directly.
  3. num_rows works because the default mode stores the result.
  4. Keep multi_query for migrations, seed scripts or multi-set procedures.

Frequently Asked Questions

It runs one string that may contain several SQL statements separated by semicolons.
Loop with mysqli_more_results(), mysqli_next_result() and mysqli_store_result() or mysqli_use_result().
It is risky. Untrusted text can inject extra statements. Prefer prepared statements for values.
Single-statement query() is simpler, returns the result immediately and is what most app code needs.
No. true mainly covers the first statement. Check later statements while draining results.

Next: mysqli_next_result()

mysqli_next_result() moves the connection to the next result from a mysqli_multi_query() b.

mysqli_next_result() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful