PHP mysqli_store_result() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_store_result() transfers the full result set of the previous query into a buffered mysqli_result on the client.

Buffered results support num_rows, data_seek, and running other queries on the same connection while you still hold the result. Contrast with mysqli_use_result(), which streams rows unbuffered and blocks other commands until you finish. mysqli_query() buffers by default (MYSQLI_STORE_RESULT).

Syntax

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

PHP
$result = mysqli_store_result($mysql);
CallWhat it does
mysqli_store_result($link)Buffers the last result set after real_query/multi_query style execution.
mysqli_use_result($link)Unbuffered alternative — do not mix the two for the same result.
$mysqli->store_result()Object-oriented form.

After mysqli_real_query() or when walking multi_query results, call store_result() or use_result() to obtain the mysqli_result. For ordinary mysqli_query() without MYSQLI_USE_RESULT, buffering already happened.

Library declarations
PHP
mysqli_store_result(mysqli $mysql, int $mode = 0): mysqli_result|false

Parameters

ParameterDescription
mysqlRequired. Procedural style only: a mysqli connection with a pending result.
modeOptional. Result mode flag (commonly left at the default 0).

Return Value

Return ValueDescription
mysqli_resultA buffered result set.
falseNo result could be stored, or an error occurred.

PHP Examples

Example 1 — Buffer a Result After real_query

Demo flow with fixed row-count output.

PHP
<?php
echo 'mysqli_real_query($link, 'SELECT id, name FROM users')', PHP_EOL;
echo 'result = mysqli_store_result($link)', PHP_EOL;
echo 'num_rows = 2', PHP_EOL;

How It Works

  1. Send SQL with real_query (or handle multi_query results).
  2. store_result() pulls the full set to the client.
  3. num_rows and data_seek work on buffered results.
  4. Sample output when connected to a demo database shows the real row count.

Example 2 — Buffered vs Unbuffered

Side-by-side teaching contrast.

PHP
<?php
echo 'store_result: buffered, seek/num_rows OK, other queries OK', PHP_EOL;
echo 'use_result:   streamed, no seek, connection busy until done', PHP_EOL;

How It Works

  1. Choose store_result for typical app code.
  2. Choose use_result only when you must stream huge sets carefully.
  3. Never leave an unbuffered result open and run another query.
  4. mysqli_query() defaults to the buffered style.

Example 3 — Free When Finished

Remind learners to release buffered memory.

PHP
<?php
echo 'fetch rows...', PHP_EOL;
echo 'mysqli_free_result($result)', PHP_EOL;

How It Works

  1. Fetch what you need from the mysqli_result.
  2. Call free() / mysqli_free_result() when done.
  3. OO result objects also offer close/free helpers.
  4. This keeps memory tidy on larger results.

Frequently Asked Questions

It buffers the previous query’s result set into a mysqli_result on the client.
store_result buffers everything; use_result streams rows and keeps the connection busy until finished.
Usually no. mysqli_query() already stores results unless you pass MYSQLI_USE_RESULT.
After real_query() or when retrieving result sets from multi_query processing.
Yes. Seeking and num_rows need a buffered result from store_result (or default query).

Next: mysqli_thread_id()

mysqli_thread_id() returns the connection’s thread ID as assigned by the MySQL server.

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