mysqli_result::field_seek is a method of the mysqli_result class in PHP, which is used to locate the field pointer to the specified column index position. This function is mainly used when you need to traverse or access field metadata (such as field names, types, etc.), and is often used in conjunction with functions such as mysqli_fetch_field() or mysqli_fetch_field_direct() .
bool mysqli_result::field_seek(int $index)
$index : This is the only parameter, type is an integer, indicating the index of the field you want to target. The index starts at 0 .
Returns true on success and false on failure.
The parameter $index is the field index, which refers to the sequential number of fields in the result set. For example, if you execute SQL like this:
SELECT id, name, email FROM users
but:
$index = 0 corresponding field id
$index = 1 corresponding field name
$index = 2 corresponding field email
The field_seek($index) method does not return field information, but moves the internal field pointer to the specified position. After that, you can obtain the information of the field through mysqli_fetch_field() .
Here is a complete usage example showing how to use field_seek() to get meta information for a specific field:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$query = "SELECT id, username, email FROM users";
$result = $mysqli->query($query);
if ($result) {
// Suppose we want to get the index as 1 Fields of(Right now username)Information
$result->field_seek(1);
$field_info = $result->fetch_field();
echo "Field name: " . $field_info->name . "<br>";
echo "Field Type: " . $field_info->type . "<br>";
echo "Maximum length: " . $field_info->max_length . "<br>";
} else {
echo "Query failed: " . $mysqli->error;
}
$mysqli->close();
?>
The output may be:
Field name: username
Field Type: 253
Maximum length: 30
illustrate:
The field type is an integer constant corresponding to the MYSQLI_TYPE_* constant.
max_length is the maximum length of the value of this field in the result set.
Dynamically generate table headers : When you need to dynamically generate HTML table headers based on database fields, field_seek() can be used to locate field meta information in sequence.
Field permission judgment : Determine whether the user has the authority to view or edit certain fields based on the field name and type.
Building ORM or framework underlying : When implementing a custom ORM system, low-level access and cache field information is required. This function is very useful.
field_seek() just changes the position of the field pointer and will not return field information. You must use it in conjunction with fetch_field() .
Indexing exceeds the total number of fields will cause the function to return false .
Applicable to result sets after SELECT query, not for INSERT , UPDATE , or DELETE operations.