PHP mysqli_field_seek() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_field_seek() moves the field cursor inside a mysqli_result to a given zero-based column index so the next mysqli_fetch_field() starts there.

It does not change which data row you are on—only which field definition fetch_field() will return next. The object style is $result->field_seek($index).

Syntax

Call mysqli_field_seek() with the values you need. Start with this line.

PHP
mysqli_field_seek($result, 0);
CallWhat it does
mysqli_field_seek($result, 0)Moves the field cursor back to the first column.
mysqli_field_seek($result, 2)Points fetch_field() at the third column next.
mysqli_field_seek($result, 0) && mysqli_fetch_field($result)Seeks to the start, then reads that field's metadata.

This is a mysqli_result API. Pair it with mysqli_fetch_field() when you need to re-read column metadata. For a specific index without seeking, use mysqli_fetch_field_direct().

Library declarations
PHP
mysqli_field_seek(mysqli_result $result, int $index): bool

Parameters

ParameterDescription
resultRequired. The mysqli_result whose field cursor you want to move.
indexRequired. Zero-based field position to seek to.

Return Value

Return ValueDescription
booltrue on success, or false if the index is out of range.

PHP Examples

Example 1 — Rewind the Field Cursor

Read one field, seek back to 0, and read the first field name again from a sample result.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products LIMIT 1");

mysqli_fetch_field($result);
mysqli_field_seek($result, 0);
$field = mysqli_fetch_field($result);

echo $field->name, PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. The first fetch_field() consumes the id metadata.
  2. mysqli_field_seek($result, 0) moves back to the start.
  3. The next fetch_field() returns id again.
  4. Sample output reprints id.

Example 2 — Seek to a Middle Column

Jump to index 1 so the next fetch_field() returns the name column.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name, price FROM products LIMIT 1");

mysqli_field_seek($result, 1);
$field = mysqli_fetch_field($result);

echo $field->name, PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. Index 1 is the second column in the SELECT list.
  2. field_seek() points the cursor there.
  3. fetch_field() returns the name metadata object.
  4. You did not need to fetch id first.

Example 3 — Invalid Seek Returns False

Try an out-of-range index and print the boolean result.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id FROM products LIMIT 1");

$ok = mysqli_field_seek($result, 5);
var_dump($ok);

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. The result has only index 0.
  2. Seeking to 5 fails.
  3. mysqli_field_seek() returns false.
  4. Check the return value before relying on the cursor.

Frequently Asked Questions

It moves the field metadata cursor in a mysqli_result to a zero-based column index for the next mysqli_fetch_field() call.
No. It only affects which column definition fetch_field() returns next. Use mysqli_data_seek() for rows.
Call $result->field_seek($index) on a mysqli_result object.
When you want one field by index without changing a cursor. field_seek() is for repositioning before fetch_field().
mysqli_field_seek() returns false and the cursor is not moved to that invalid position.

Next: mysqli_free_result()

mysqli_free_result() frees the memory associated with a mysqli_result. After you finish re.

mysqli_free_result() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful