PHP mysqli_field_seek() Function

mysqli_get_charset()
PHP mysqli field_count() Function

Mysqli_field_seek() is a MySQLi extension for PHP function that is used to set the internal field pointer of a result set to a certain field. When working with big result sets and needing to access specific fields without iterating through the full set, this method comes in handy.

Syntax:

mysqli_field_seek(mysqli_result $result, int $fieldnr);
  • $result: The result set returned by a MySQLi query.
  • $fieldnr: The numerical index of the field you want to set the pointer to. The first field has an index of 0.

Examples:

Example 1.

// Connect to the database
$link = mysqli_connect("localhost", "username", "password", "database");

// Execute a query
$result = mysqli_query($link, "SELECT name, email FROM users");

// Set the pointer to the second field (index 1)
mysqli_field_seek($result, 1);

// Fetch the field information
$field = mysqli_fetch_field($result);

// Print the field name
echo "The second field is named " . $field->name;

The first example shows how to use the mysqli_field_seek function to change the internal field pointer to the second field (index 1) of a result set and then use the mysqli_fetch_field function to receive information about that field.

The code initially connects to the database with mysqli_connect before running a query to retrieve the user’s name and email address from the users table. The mysqli_field_seek function is then used to set the internal field reference to the second field (index 1). Following that, it invokes the mysqli_fetch_field function to retrieve the field information and outputs the field name.

Example 2.

// Connect to the database
$link = mysqli_connect("localhost", "username", "password", "database");

// Execute a query
$result = mysqli_query($link, "SELECT name, email FROM users");

// Loop through the result set
while($row = mysqli_fetch_assoc($result)) {
    // Set the pointer to the first field (index 0)
    mysqli_field_seek($result, 0);

    // Fetch the field information
    $field = mysqli_fetch_field($result);

    // Print the field name and the corresponding value
    echo "The first field is named " . $field->name . " and the value is " . $row[$field->name] . "<br>";
}

The second example shows how to traverse through the result set and collect information about the first field for each row by using the mysqli_field_seek function in a loop.

The code initially connects to the database with mysqli_connect before running a query to retrieve the user’s name and email address from the users table. Then, using a while loop, it iterates through the result set, retrieving each row with mysqli_fetch_assoc. The mysqli_field_seek function is called within the loop to set the internal field pointer to the first field (index 0) for each row. Following that, it invokes the mysqli_fetch_field function to retrieve the field information, and it outputs the field name and value for each row.

mysqli_get_charset()
PHP mysqli field_count() Function

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top