Moves a buffered result cursor to a specified zero-based row number.
The examples focus on the operation itself, including the important distinction: fetch_assoc advances sequentially from the current cursor.
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_result::data_seek(...) — 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. Result methods operate on a mysqli_result produced by an actual SELECT.
<?php
$mysqli = new mysqli(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
$mysqli->set_charset('utf8mb4');
$result = $mysqli->query('SELECT id, email FROM users ORDER BY id LIMIT 3');
if ($result->num_rows > 1) { $result->data_seek(1); echo json_encode($result->fetch_assoc()); }
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. Result methods operate on a mysqli_result produced by an actual SELECT. The SELECT filters a different subset of rows.
<?php
$mysqli = new mysqli(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
$mysqli->set_charset('utf8mb4');
$result = $mysqli->query('SELECT id, email FROM users WHERE active = 1 ORDER BY id LIMIT 2');
if ($result->num_rows > 1) { $result->data_seek(1); echo json_encode($result->fetch_assoc()); }
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. Result methods operate on a mysqli_result produced by an actual SELECT. The SELECT filters a different subset of rows. This variation seeks to the first row instead of the second.
<?php
$mysqli = new mysqli(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
$mysqli->set_charset('utf8mb4');
$result = $mysqli->query('SELECT id, email FROM users WHERE id > 10 ORDER BY id LIMIT 5');
if ($result->num_rows > 0) { $result->data_seek(0); echo json_encode($result->fetch_assoc()); }
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
The result must be buffered and the offset within 0..num_rows-1.
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
| Approach | Difference |
|---|---|
| mysqli_result::data_seek | Moves a buffered result cursor to a specified zero-based row number. |
| Related API or alternative | fetch_assoc advances sequentially from the current cursor. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | The result must be buffered and the offset within 0..num_rows-1. |
| Confusing it with a related API | fetch_assoc advances sequentially from the current cursor. |
Key Takeaways
- Moves a buffered result cursor to a specified zero-based row number.
- The result must be buffered and the offset within 0..num_rows-1.
- fetch_assoc advances sequentially from the current cursor.
Use mysqli_result::data_seek when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.