PHP mysqli_fetch_row() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_fetch_row() reads the next row from a mysqli_result and returns it as a numeric array. Column positions start at 0.

It matches mysqli_fetch_array($result, MYSQLI_NUM). When there are no more rows it returns null. The object style is $result->fetch_row().

Syntax

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

PHP
$row = mysqli_fetch_row($result);
CallWhat it does
mysqli_fetch_row($result)Returns the next row as a 0-based numeric array.
$row[0]Reads the first column of the fetched row.
while ($row = mysqli_fetch_row($result)) { … }Loops until every row has been read.

Numeric keys are useful for short SELECT lists where you know the column order. For named access, prefer mysqli_fetch_assoc(). This is a mysqli_result API: $result->fetch_row() in object style.

Library declarations
PHP
mysqli_fetch_row(mysqli_result $result): array|null|false

Parameters

ParameterDescription
resultRequired. A mysqli_result with unread rows.

Return Value

Return ValueDescription
array|null|falseA numeric array for the next row, null when finished, or false on failure.

PHP Examples

Example 1 — Read One Numeric Row

Fetch a single row and print columns by index from a fixed sample.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products WHERE id = 1");
$row = mysqli_fetch_row($result);

echo $row[0], " ", $row[1], PHP_EOL;

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

How It Works

  1. The SELECT returns id then name.
  2. mysqli_fetch_row() stores them at indexes 0 and 1.
  3. We print both values with a space between.
  4. Sample output shows the first demo product.

Example 2 — Empty Result Returns Null

When no row matches, the first fetch_row() call returns null.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id FROM products WHERE id = 0");
$row = mysqli_fetch_row($result);

var_dump($row);

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

How It Works

  1. The WHERE clause matches no sample row.
  2. mysqli_fetch_row() has nothing to return.
  3. The result is null (shown as NULL by var_dump).
  4. Check for null before using indexes.

Example 3 — Loop with Numeric Indexes

Print the first column of each row until the result ends.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT name FROM products ORDER BY id LIMIT 3");

while ($row = mysqli_fetch_row($result)) {
    echo $row[0], PHP_EOL;
}

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

How It Works

  1. Each row has a single column at index 0.
  2. The while loop fetches until null.
  3. Only $row[0] is printed each time.
  4. Three sample names appear.

Frequently Asked Questions

It returns the next result row as a numeric array starting at index 0, or null when there are no more rows.
fetch_row() uses numbers as keys. fetch_assoc() uses column names.
Call $result->fetch_row() on a mysqli_result object.
Yes. It matches mysqli_fetch_array($result, MYSQLI_NUM).
When the SELECT list is short and stable and you are fine reading $row[0], $row[1], and so on.

Next: mysqli_field_count()

mysqli_field_count() returns how many columns the most recent query on a connection produc.

mysqli_field_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