PHP mysqli_use_result() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_use_result() opens an unbuffered result set for the previous query so rows stream from the server as you fetch them.

Unbuffered mode saves client memory on huge results, but you cannot data_seek, and you must finish fetching before sending another command on that connection or you risk "Commands out of sync". mysqli_store_result() is the buffered alternative beginners should prefer.

Syntax

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

PHP
$result = mysqli_use_result($mysql);
CallWhat it does
mysqli_use_result($link)Starts an unbuffered result after real_query / USE_RESULT style execution.
mysqli_store_result($link)Buffered alternative with seek/num_rows support.
$mysqli->use_result()Object-oriented form.

Do not use use_result() if you will pause a long time between rows while holding the result — that can stall the server. For normal apps, mysqli_query() with default buffering is simpler.

Library declarations
PHP
mysqli_use_result(mysqli $mysql): mysqli_result|false

Parameters

ParameterDescription
mysqlRequired. Procedural style only: a mysqli connection with a pending result.

Return Value

Return ValueDescription
mysqli_resultAn unbuffered result object.
falseOpening the unbuffered result failed.

PHP Examples

Example 1 — Stream Rows Unbuffered

Demo loop with fixed sample rows (no live server).

PHP
<?php
$rows = [
    ['id' => 1, 'name' => 'Ada'],
    ['id' => 2, 'name' => 'Grace'],
];
foreach ($rows as $row) {
    echo $row['id'], ' ', $row['name'], PHP_EOL;
}

How It Works

  1. After real_query, call mysqli_use_result($link).
  2. Fetch rows one by one without buffering everything first.
  3. This demo prints stand-in rows instead of talking to MySQL.
  4. Sample output when connected to a demo database looks the same after fetch_assoc().

Example 2 — Commands Out of Sync Risk

Explain why a second query fails while unbuffered results remain.

PHP
<?php
echo 'use_result open -> fetch rows...', PHP_EOL;
echo 'second query now -> Commands out of sync', PHP_EOL;
echo 'finish/free first result before next query', PHP_EOL;

How It Works

  1. Unbuffered mode keeps the connection busy.
  2. Another query too early triggers out-of-sync errors.
  3. Fetch all rows and free the result first.
  4. store_result avoids this class of beginner bugs.

Example 3 — Choose store_result by Default

Teaching recommendation in one glance.

PHP
<?php
echo 'default choice: store_result / mysqli_query()', PHP_EOL;
echo 'special case:   use_result for huge streams', PHP_EOL;

How It Works

  1. Buffered mode is easier for tutorials.
  2. Unbuffered mode is an advanced memory trade-off.
  3. Measure before you switch a production query.
  4. Always free results in either mode.

Frequently Asked Questions

It returns an unbuffered mysqli_result that streams rows from the server as you fetch them.
Buffered results support seeking and let you run other queries sooner. They are simpler for most apps.
No. Seeking needs a buffered result from store_result or default mysqli_query().
An error you often see if you start another query while an unbuffered result is still open.
Only if you pass MYSQLI_USE_RESULT. The default is buffered store mode.

Next: mysqli_warning_count()

mysqli_warning_count() returns how many warnings the last MySQLi operation produced on the.

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