PHP Function Reference

PHP mysqli_result data_seek() Method



The PHP mysqli_result::data_seek() / mysqli_data_seek() function is used to adjust the result pointer to an arbitrary row specified by the offset in the result set.

Syntax

//Object-oriented style
public mysqli_result::data_seek(offset)

//Procedural style
mysqli_data_seek(result, offset)

Parameters

result Required. For procedural style only: Specify a mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result().
offset Required. Specify the field offset. It must be between zero and (total number of rows - 1).

Return Value

Returns true on success or false on failure.

Example: Object-oriented style

The example below shows the usage of mysqli_result::data_seek() method.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//getting query result from the database
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
$result = $mysqli->query($sql);

//seeking to row number 51
$result->data_seek(50);

//fetching a single row
$row = $result->fetch_row();

printf("Name: %s, Age: %d\n", $row[0], $row[1]);

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

Name: John, Age: 28

Example: Procedural style

The example below shows the usage of mysqli_data_seek() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//getting query result from the database
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
$result = mysqli_query($mysqli, $sql);

//seeking to row number 51
mysqli_data_seek($result, 50);

//fetching a single row
$row = mysqli_fetch_row($result);

printf("Name: %s, Age: %d\n", $row[0], $row[1]);

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

Name: John, Age: 28

Example: adjusting the result pointer when iterating

This function can be useful when iterating over the result set to impose a custom order or rewind the result set when iterating multiple times. Consider the example below:

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//getting query result from the database
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
$result = $mysqli->query($sql);

//iterating the result set in reverse order
for ($row_no = $result->num_rows - 1; $row_no >= 0; $row_no--) {
  $result->data_seek($row_no);

  //fetching a single row
  $row = $result->fetch_row();

  printf("Name: %s, Age: %d\n", $row[0], $row[1]);
}

//resetting pointer to the 
//beginning of the result set
$result->data_seek(0);

echo "\n";

//iterating the same result set again
while ($row = $result->fetch_row()) {
  printf("Name: %s, Age: %d\n", $row[0], $row[1]);
}

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

Name: Adam, Age: 28
Name: John, Age: 27
Name: Kim, Age: 26
Name: Marry, Age: 23

Name: Marry, Age: 23
Name: Kim, Age: 26
Name: John, Age: 27
Name: Adam, Age: 28

❮ PHP MySQLi Reference