PHP Function Reference

PHP mysqli_stmt data_seek() Method



The PHP mysqli_stmt::data_seek() / mysqli_stmt_data_seek() function is used to seek to an arbitrary result pointer in the statement result set.

Note: mysqli_stmt_store_result() must be called prior to mysqli_stmt_data_seek().

Syntax

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

//Procedural style
mysqli_stmt_data_seek(statement, offset)

Parameters

statement Required. For procedural style only: Specify a mysqli_stmt object returned by mysqli_stmt_init().
offset Required. Specify the field offset. It must be between zero and (total number of rows - 1).

Return Value

No value is returned.

Example: Object-oriented style

The example below shows the usage of mysqli_stmt::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();
}

//preparing an SQL statement for execution
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
if ($stmt = $mysqli->prepare($sql)) {

  //executing query
  $stmt->execute();

  //binding result variables
  $stmt->bind_result($name, $age);

  //storing result
  $stmt->store_result();

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

  //fetching values
  $stmt->fetch();

  printf("Name: %s, Age: %d\n", $name, $age);

  //closing statement
  $stmt->close();
}

//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_stmt_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();
}

//preparing an SQL statement for execution
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
if ($stmt = mysqli_prepare($mysqli, $sql)) {

  //executing query
  mysqli_stmt_execute($stmt);

  //binding result variables
  mysqli_stmt_bind_result($stmt, $name, $age);

  //storing result
  mysqli_stmt_store_result($stmt);

  //seeking to row number 51
  mysqli_stmt_data_seek($stmt, 50);

  //fetching values
  mysqli_stmt_fetch($stmt);

  printf("Name: %s, Age: %d\n", $name, $age);

  //closing statement
  mysqli_stmt_close($stmt);
}

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

The output of the above code will be similar to:

Name: John, Age: 28

❮ PHP MySQLi Reference