When using PHP's MySQLi extension for database operations, we usually read data from the result set. However, sometimes we not only need to obtain data rows, but also want to access the metadata of the field, such as field name, field type, etc. The mysqli_result::field_seek() function is a tool that comes in handy in this case. This article will introduce in detail the purpose, usage method and practical application scenarios of this function.
mysqli_result::field_seek(int $field_number): bool
The purpose of this method is to move the field pointer to the specified field number and prepare for subsequent call to the fetch_field() method. Field numbers start at 0.
This function returns a Boolean value indicating whether the field pointer is successfully set.
When analyzing the field information of the result set, we often use field_seek() with fetch_field() . The following is a specific example:
<code> <?php $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$result = $mysqli->query("SELECT id, name, email FROM users");
if ($result) {
// Position to the first field (i.e. id)
$result->field_seek(0);
$field = $result->fetch_field();
echo "field name: " . $field->name . "<br>";
// Positioning to the second field(Right nowname)
$result->field_seek(1);
$field = $result->fetch_field();
echo "Field name: " . $field->name . "<br>";
// Close result sets and connections
$result->close();
}
$mysqli->close();
?>
</code>
field_seek() itself does not return field information, but is used to set the internal field pointer. The work of truly extracting field details is done by fetch_field() . Therefore, these two functions are usually used together.
Note: fetch_field() will return the field information object pointed to by the current field pointer, which contains the field name, table name, length, type and other properties.
When building a dynamic form or API output structure, we often need to know the name, data type and other information of each field. For example, in a form build tool, you can get the field name in the following ways:
<code> foreach (range(0, $result->field_count - 1) as $i) { $result->field_seek($i); $field = $result->fetch_field(); echo "Field$i: " . $field->name . "<br>"; } </code>When debugging complex queries, developers may need to log or format output the field structure in the result set. In this scenario , field_seek() + fetch_field() is a very practical combination.
For building lightweight ORM tools, developers may dynamically generate class attributes by reading field information, or map fields into model structures.
Field numbers start from 0 : If a number greater than the total number of fields is set, field_seek() will return false .
Result set type limitation : It is only meaningful if the query returns field information (such as SELECT statements); for operations like INSERT and UPDATE , it is useless.
Resource release : After using the mysqli_result object, close() method should be called to release the resource.
The following example is used to get the types of all fields in a query result:
<code> $mysqli = new mysqli("localhost", "user", "password", "database"); $result = $mysqli->query("SELECT * FROM posts"); if ($result) {
for ($i = 0; $i < $result->field_count; $i++) {
$result->field_seek($i);
$field = $result->fetch_field();
echo "Field name: {$field->name}, type: {$field->type}<br>";
}
$result->close();
}
$mysqli->close();
</code>
mysqli_result::field_seek() is a very practical but often overlooked function. It allows us to flexibly obtain information of arbitrary fields, especially suitable for building general query processing logic. Although we can use functions such as fetch_assoc() in most scenarios, field_seek() provides irreplaceable convenience when you need to understand the field structure.
To gain insight into more advanced features of MySQLi, you can refer to the official documentation or visit https://gitbox.net/docs/php/mysqli for relevant information.