PHP mysqli_num_rows() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_num_rows() returns how many rows are in a result set object from a SELECT-style query.

It needs a buffered (stored) result. With the default mysqli_query() mode, or after mysqli_store_result(), the count is available immediately. Unbuffered results from MYSQLI_USE_RESULT / use_result() cannot give a reliable count until rows are fetched.

Syntax

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

PHP
$n = mysqli_num_rows($result);
CallWhat it does
mysqli_num_rows($result)Returns the row count for a stored mysqli_result.
echo mysqli_num_rows(mysqli_query($mysqli, "SELECT id FROM t"))Counts rows from a default buffered query().
if (mysqli_num_rows($result) === 0) { echo "none"; }Branches when the SELECT matched nothing.

Pass a mysqli_result, not the connection. For INSERT/UPDATE/DELETE row counts use mysqli_affected_rows() on the connection instead.

Library declarations
PHP
mysqli_num_rows(mysqli_result $result): int|string

Parameters

ParameterDescription
resultRequired. A mysqli_result from mysqli_query(), mysqli_store_result(), or similar.

Return Value

Return ValueDescription
int|stringThe number of rows in the result. Very large counts may be returned as a string.

PHP Examples

Example 1 — Count Rows From a Buffered Query

Default mysqli_query() stores the result, so num_rows works immediately.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
$result = mysqli_query($mysqli, "SELECT id, name FROM users LIMIT 5");

echo "Rows: ", mysqli_num_rows($result), PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. mysqli_query() uses MYSQLI_STORE_RESULT by default.
  2. mysqli_num_rows() reads the buffered row count.
  3. The sample SELECT returns five rows.
  4. Free the result when finished.

Example 2 — Empty Result Is Zero, Not False

A valid SELECT with no matches still returns a result object with 0 rows.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
$result = mysqli_query($mysqli, "SELECT id FROM users WHERE 1 = 0");

var_dump(mysqli_num_rows($result));

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. The query succeeds but matches nothing.
  2. num_rows returns 0.
  3. That is different from query() returning false on SQL errors.
  4. Check the query return value for failures first.

Example 3 — Unbuffered Mode Blocks a Useful Count

Show why beginners should stick to stored results for num_rows.

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

// Prefer the default stored mode when you need num_rows:
$result = mysqli_query($mysqli, "SELECT id FROM users", MYSQLI_STORE_RESULT);
echo "Stored count: ", mysqli_num_rows($result), PHP_EOL;
mysqli_free_result($result);

// MYSQLI_USE_RESULT is unbuffered — avoid num_rows until all rows are fetched.
echo "Use store_result / default query() for counts.", PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. MYSQLI_STORE_RESULT (the default) buffers rows on the client.
  2. num_rows can then return 12 in this sample.
  3. MYSQLI_USE_RESULT streams rows and is a poor fit for early counts.
  4. Beginners should keep the default unless they have a large-result reason.

Frequently Asked Questions

The number of rows in a mysqli_result from a SELECT-style query.
The count is known up front only when rows are buffered with store_result or default query(). Unbuffered use_result mode does not support a reliable early count.
No. Pass the mysqli_result object. For write statements use mysqli_affected_rows($mysqli).
query() returns false, so there is no result for num_rows. Check the return value before counting.
No. 0 means the statement returned an empty set.

Next: mysqli_options()

mysqli_options() sets a connection option on a mysqli handle created with mysqli_init(), b.

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