Skip to content

field_seek

This article covers the mysqli_field_seek() function in PHP, which sets the field cursor to a specified field offset in a MySQLi result set. We will explain how it works, document its parameters and return values, and provide usage examples.

Introduction to the mysqli_field_seek() function

The mysqli_field_seek() function is a built-in PHP function that sets the field cursor to a specified field offset in a MySQLi result set. It is useful when you need to access a specific column by its offset.

Note: This function is rarely used in modern PHP development, as most developers prefer fetching rows into associative arrays or objects.

Parameters:

  • result: A MySQLi result set object returned by mysqli_query().
  • field: An integer representing the field offset (0-based index).

Return Value: Returns true on success, false on failure.

How to use the mysqli_field_seek() function

Using the mysqli_field_seek() function is straightforward. You call it on a valid MySQLi result set and specify the field offset you want to set the cursor to. Here is an example:

How to use the mysqli_field_seek() function?

php
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");

if (!$mysqli) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);

if (!$result) {
    die("Query failed: " . mysqli_error($mysqli));
}

mysqli_field_seek($result, 2);
$field_info = mysqli_fetch_field($result);
if ($field_info) {
    printf("Field name: %s\n", $field_info->name);
}

mysqli_close($mysqli);
?>

In this example, we connect to a MySQL database and execute a query. Basic error handling is added to check for connection and query failures. We then call mysqli_field_seek() on the result set to move the field cursor to the third field (offset 2). Finally, mysqli_fetch_field() retrieves information about the current field, and printf() outputs its name.

Conclusion

The mysqli_field_seek() function allows you to reposition the field cursor within a MySQLi result set. While it is rarely needed in modern PHP applications, understanding it can be helpful when working with legacy code or specific MySQLi workflows.

Practice

What does the mysqli_result::data_seek() function in PHP do?

Dual-run preview — compare with live Symfony routes.