Content · Programming · PHP

PHP mysqli_num_rows() Function: Usage and Examples

Returns the number of buffered rows in a mysqli_result. For prepared statements, results may need buffering or get_result before counting.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

Returns the number of buffered rows in a mysqli_result.

The examples focus on the operation itself, including the important distinction: mysqli_affected_rows reports modified rows from write statements.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Syntax and Return Value


mysqli_num_rows(...) — requires the corresponding optional extension

The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.

Example 1: Basic Use


Examples assume a demo database containing users(id, email, active), products(id, name, price), and orders(id, user_id, total) as required by the query. Counts rows already present in a buffered query result.

<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
$result = mysqli_query($link, 'SELECT id, email FROM users ORDER BY id LIMIT 3');
echo mysqli_num_rows($result);

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 2: Checking Behavior


Examples assume a demo database containing users(id, email, active), products(id, name, price), and orders(id, user_id, total) as required by the query. Counts rows already present in a buffered query result. The SELECT filters a different subset of rows.

<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
$result = mysqli_query($link, 'SELECT id, email FROM users WHERE active = 1 ORDER BY id LIMIT 2');
echo mysqli_num_rows($result);

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 3: Practical Example


Examples assume a demo database containing users(id, email, active), products(id, name, price), and orders(id, user_id, total) as required by the query. Counts rows already present in a buffered query result. The SELECT filters a different subset of rows.

<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
$result = mysqli_query($link, 'SELECT id, email FROM users WHERE id > 10 ORDER BY id LIMIT 5');
echo mysqli_num_rows($result);

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Important Behavior and Edge Cases


For prepared statements, results may need buffering or get_result before counting.

This API is supplied by an optional extension that is not installed in the local test runtime, or requires a different PHP version. The example must be verified in a matching environment.

Comparison with Related Approaches


ApproachDifference
mysqli_num_rowsReturns the number of buffered rows in a mysqli_result.
Related API or alternativemysqli_affected_rows reports modified rows from write statements.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsFor prepared statements, results may need buffering or get_result before counting.
Confusing it with a related APImysqli_affected_rows reports modified rows from write statements.

Key Takeaways


  • Returns the number of buffered rows in a mysqli_result.
  • For prepared statements, results may need buffering or get_result before counting.
  • mysqli_affected_rows reports modified rows from write statements.

Use mysqli_num_rows when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.