MySQLi Tutorial MySQLi References

mysqli_result field_seek() Method



The mysqli_result::field_seek() / mysqli_field_seek() function sets the field cursor to the given offset. The next call to mysqli_fetch_field() will retrieve the field definition of the column associated with that offset.

Note: To seek to the beginning of a row, pass an offset value of zero.

Syntax

//Object-oriented style
public mysqli_result::field_seek(index)

//Procedural style
mysqli_field_seek(result, index)

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().
index Required. Specify the field number. This value must be in the range from 0 to (number of fields - 1).

Return Value

Returns true on success or false on failure.

Example: Object-oriented style

The example below shows the usage of mysqli_result::field_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 EmpId, Name, Age FROM Employee ORDER BY Age";

if ($result = $mysqli->query($sql)) {

  //getting field information for second column
  $result->field_seek(1);
  $finfo = $result->fetch_field();

  printf("Name:     %s\n", $finfo->name);
  printf("Table:    %s\n", $finfo->table);
  printf("max. Len: %d\n", $finfo->max_length);

  //free the memory associated with the result
  $result->close();
}

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

The output of the above code will be similar to:

Name:     Name
Table:    Employee
max. Len: 50

Example: Procedural style

The example below shows the usage of mysqli_field_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 EmpId, Name, Age FROM Employee ORDER BY Age";

if ($result = mysqli_query($mysqli, $sql)) {

  //getting field information for second column
  mysqli_field_seek($result, 1);
  $finfo = mysqli_fetch_field($result);

  printf("Name:     %s\n", $finfo->name);
  printf("Table:    %s\n", $finfo->table);
  printf("max. Len: %d\n", $finfo->max_length);

  //free the memory associated with the result
  mysqli_free_result($result);
}

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

The output of the above code will be similar to:

Name:     Name
Table:    Employee
max. Len: 50

❮ MySQLi Functions Reference