PHP mysqli_next_result() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_next_result() moves the connection to the next result from a mysqli_multi_query() batch and returns true on success or false on failure.

Call it only when mysqli_more_results() is true. After a successful move, use store_result() or use_result() again if that next statement produced rows.

Syntax

Call mysqli_next_result() with the values you need. Start with this line.

PHP
$ok = mysqli_next_result($mysqli);
CallWhat it does
mysqli_next_result($mysqli)Advances to the next multi-query result.
mysqli_more_results($mysqli) && mysqli_next_result($mysqli)Safe advance used in drain loops.
if (!mysqli_next_result($mysqli)) { echo mysqli_error($mysqli); }Surfaces an error from a later statement.

Forget next_result and the connection stays busy. This is the usual cause of "Commands out of sync" after multi_query or some stored procedures.

Library declarations
PHP
mysqli_next_result(mysqli $mysql): bool

Parameters

ParameterDescription
mysqlRequired. The mysqli connection processing a multi-statement result queue.

Return Value

Return ValueDescription
booltrue when the next result was prepared successfully; false on error (check mysqli_error()).

PHP Examples

Example 1 — Advance From First Set to Second

Read one set, call next_result, then read the next.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_multi_query($mysqli, "SELECT 'first' AS label; SELECT 'second' AS label;");

$r1 = mysqli_store_result($mysqli);
$row1 = mysqli_fetch_assoc($r1);
mysqli_free_result($r1);

mysqli_next_result($mysqli);

$r2 = mysqli_store_result($mysqli);
$row2 = mysqli_fetch_assoc($r2);
mysqli_free_result($r2);

echo $row1["label"], " -> ", $row2["label"], PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. store_result loads the first SELECT.
  2. Free that result before moving on.
  3. next_result prepares the second statement's result.
  4. A second store_result reads "second".

Example 2 — Loop Until No More Results

Combine more_results and next_result in the standard while condition.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_multi_query($mysqli, "SELECT 1; SELECT 2; SELECT 3;");

$n = 0;
do {
    if ($result = mysqli_store_result($mysqli)) {
        $n++;
        mysqli_free_result($result);
    }
} while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli));

echo $n, PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. Each successful store_result increments the counter.
  2. more_results() asks whether another set exists.
  3. next_result() advances only when needed.
  4. The sample batch had three SELECTs.

Example 3 — Error From a Later Statement

next_result can fail when a later statement in the batch is invalid.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_multi_query($mysqli, "SELECT 1; SELCT 2;");

if ($r = mysqli_store_result($mysqli)) {
    mysqli_free_result($r);
}

if (mysqli_more_results($mysqli) && !mysqli_next_result($mysqli)) {
    echo "Later statement failed.", PHP_EOL;
}

mysqli_close($mysqli);

How It Works

  1. The first SELECT works and is freed.
  2. more_results() is still true because another statement was queued.
  3. next_result() returns false for the typo "SELCT".
  4. Check mysqli_error() in real apps for the exact message.

Frequently Asked Questions

It advances a multi-query connection to the next statement result.
When mysqli_more_results() is true and you have finished with the current result.
Unread results stay pending and later queries often fail with "Commands out of sync".
No. After it succeeds, call store_result() or use_result() to get a mysqli_result when rows exist.
Not for normal single statements. It belongs with multi_query and some multi-set procedures.

Next: mysqli_num_rows()

mysqli_num_rows() returns how many rows are in a result set object from a SELECT-style que.

mysqli_num_rows() 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