In PHP, the mysqli_result::field_seek() function is used to move the field pointer within a result set. This function helps developers locate a specific field in the current result set, commonly used to access particular field data from query results. In advanced database operations, this method may be required to manipulate and read field data.
This article will provide a detailed explanation of the basic usage of the mysqli_result::field_seek() function and illustrate its practical application through examples.
<span><span><span class="hljs-keyword">bool</span></span><span> mysqli_result::</span><span><span class="hljs-title function_ invoke__">field_seek</span></span><span>(</span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$fieldnr</span></span><span>);
</span></span>
$fieldnr: An integer representing the field position to move to (starting from 0). The position is relative to the order of fields in the query result.
The function returns a boolean value:
Success: Returns true.
Failure: Returns false.
In some situations, you may need to manipulate the order of fields in a query result or retrieve data based on a field’s position. The field_seek() function is ideal for these scenarios. For example, when you need to jump to a specific field and process its data, field_seek() is useful.
Suppose we have a database table users with the fields: id, name, and email, and we want to retrieve data from a specific field.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Create connection</span></span><span>
</span><span><span class="hljs-variable">$conn</span></span> = </span><span><span class="hljs-keyword">new</span> </span><span><span class="hljs-title function_ invoke__">mysqli</span></span>("localhost", "root", "", "test_db");
<p></span>// Check connection<br>
if ($conn->connect_error) {<br>
die("Connection failed: " . $conn->connect_error);<br>
}</p>
<p>// Execute query<br>
$sql = "SELECT id, name, email FROM users";<br>
$result = $conn->query($sql);</p>
<p>if ($result->num_rows > 0) {<br>
// Move to the 2nd field (index 1, name field)<br>
$result->field_seek(1);</p>
$field_info = $result->fetch_field();
echo "Current field: " . $field_info->name . "<br>";
</span><span><span class="hljs-comment">// Fetch the current record</span></span>
$row = $result->fetch_assoc();
echo "Value of current field: " . $row[$field_info->name] . "<br>";
</span><span><span class="hljs-comment">// Move to the 3rd field (email field)</span></span>
$result->field_seek(2);
$field_info = $result->fetch_field();
echo "Current field: " . $field_info->name . "<br>";
</span><span><span class="hljs-comment">// Fetch the current record again</span></span>
echo "Value of current field: " . $row[$field_info->name] . "<br>";
} else {
echo "No records found.";
}
$conn->close();
?>
Database Connection: A connection is created using new mysqli() to connect to the test_db database.
Executing Query: The $conn->query() method executes a SELECT statement to retrieve data from the users table.
Using field_seek(): In the query result, $result->field_seek(1) moves to the second field (name), and $result->fetch_field() retrieves metadata, including the field name.
Getting Field Values: The current field’s value is accessed using $row[$field_info->name] and outputted.
Field Index: The parameter for field_seek() is the field’s index, starting at 0. Developers should confirm the field order when querying multiple fields.
Query Result: field_seek() only works on the current query result. To reuse it, the query must be executed again to get the result set.
Movement Limitation: field_seek() works only on mysqli_result objects and does not apply to other types of result sets (such as those returned by mysqli_fetch_assoc()).
The mysqli_result::field_seek() function is highly practical, allowing developers to move within a query result according to field positions. It is particularly useful when accessing and manipulating specific field data. Mastering this function makes handling MySQL query results more flexible and efficient.
With the introduction and example code in this article, readers can better understand how to use the mysqli_result::field_seek() function in real development, improving the flexibility and efficiency of database operations.
Related Tags:
mysqli_result