PHP mysqli_data_seek() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_data_seek() moves the row cursor of a buffered mysqli_result to a given offset so the next fetch starts there.

Object-oriented style uses $result->data_seek($offset). Offsets start at 0. This only works on buffered results (the default for mysqli_query), not on unbuffered use_result() streams.

Syntax

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

PHP
mysqli_data_seek($result, 0);
CallWhat it does
mysqli_data_seek($result, 0)Moves back to the first row.
mysqli_data_seek($result, 2)Moves to the third row (0-based index 2).
$result->data_seek(0)Object-oriented form.

You need a mysqli_result object. After seeking, call fetch_assoc(), fetch_row() or similar to read that row. Seeking past the end returns false.

Library declarations
PHP
mysqli_data_seek(mysqli_result $result, int $offset): bool

Parameters

ParameterDescription
resultRequired. A buffered mysqli_result.
offsetRequired. Zero-based row number to move to.

Return Value

Return ValueDescription
booltrue on success, false if the offset is out of range or the result is not seekable.

PHP Examples

Example 1 — Re-read the First Row

Fetch once, seek to 0, fetch again. Sample output when connected to a demo database.

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

$row = mysqli_fetch_assoc($result);
echo "first pass: ", $row["name"], PHP_EOL;

mysqli_data_seek($result, 0);
$row = mysqli_fetch_assoc($result);
echo "after seek: ", $row["name"], PHP_EOL;

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

How It Works

  1. query() returns a buffered mysqli_result.
  2. The first fetch_assoc() reads row 0.
  3. data_seek(0) moves the cursor back to the start.
  4. The second fetch reads the same first row again.

Example 2 — Jump to a Middle Row

Seek to offset 2 and print that product name.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$result = $mysqli->query("SELECT name FROM products ORDER BY id LIMIT 5");

$result->data_seek(2);
$row = $result->fetch_assoc();
echo $row["name"], PHP_EOL;

$result->free();
$mysqli->close();

How It Works

  1. A five-row demo result is loaded.
  2. data_seek(2) selects the third row.
  3. fetch_assoc() reads only that row.
  4. Sample output prints Stylus.

Example 3 — Out-of-Range Seek Fails

Seeking past the last row returns false.

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

$ok = mysqli_data_seek($result, 99);
echo $ok ? "moved" : "seek failed", PHP_EOL;

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

How It Works

  1. The result only has two rows (offsets 0 and 1).
  2. data_seek(99) is out of range.
  3. The function returns false.
  4. The script prints seek failed.

Frequently Asked Questions

It moves the row pointer inside a buffered result set to a zero-based offset so the next fetch starts at that row.
Call $result->data_seek($offset) on a mysqli_result object.
The offset is outside 0..num_rows-1, or the result is unbuffered and not seekable.
No. It belongs to mysqli_result. Pass the result from query() / store_result().
If you need all rows in memory as an array, fetch_all() is simpler. Use data_seek when you want to jump around without rebuilding the whole array.

Next: mysqli_debug()

mysqli_debug() turns on low-level MySQLi client debugging using a control string of flags.

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