Current Location: Home> Latest Articles> How to Use mysqli_result::fetch_field to Get Current Field Name and Type – Practical Example Guide

How to Use mysqli_result::fetch_field to Get Current Field Name and Type – Practical Example Guide

gitbox 2025-08-27

How to Use mysqli_result::fetch_field to Get Current Field Name and Type – Practical Example Guide

When programming in PHP with a MySQL database, handling query results is often necessary. The mysqli_result::fetch_field function is a very practical tool that helps developers retrieve the name and type of fields in the current query result. Below, we will walk through a detailed practical example to help you better understand how to use this function.

1. Introduction to mysqli_result::fetch_field

mysqli_result::fetch_field is a method provided by the PHP mysqli extension and belongs to the mysqli_result class. This function returns information about the current field in the result set, including the field name, data type, maximum length, and more. It returns an object containing the field information, which is useful for dynamically generating tables or further database operations.

Syntax:

<span><span><span class="hljs-keyword">public</span></span><span> mysqli_field_object mysqli_result::</span><span><span class="hljs-variable constant_">fetch_field</span></span><span> ( </span><span><span class="hljs-keyword">void</span></span><span> )
</span></span>

2. Object Properties for Field Information

The fetch_field method returns an object of type mysqli_field_object, which contains the following key properties:

  • name: The name of the field.

  • table: The table to which the field belongs.

  • max_length: The maximum length of the field.

  • length: The actual data length of the field.

  • charsetnr: The character set number of the field.

  • flags: Field flags (e.g., whether it is a unique key).

  • type: The data type of the field (such as MYSQLI_TYPE_STRING, MYSQLI_TYPE_INT, etc.).

3. Practical Example: Retrieving Field Name and Type

Here is a basic example using the mysqli_result::fetch_field function, showing how to retrieve field names and types from a query result.

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Connect to the database</span></span><span>
</span><span><span class="hljs-variable">$mysqli</span></span> = <span><span class="hljs-keyword">new</span></span><span> <span class="hljs-title function_ invoke__">mysqli</span>(<span class="hljs-string">"localhost"</span>, <span class="hljs-string">"root"</span>, <span class="hljs-string">"password"</span>, <span class="hljs-string">"test_db"</span>);
<p></span>// Check connection<br>
</span>if ($mysqli->connect_error) {<br>
die("Connection failed: " . $mysqli->connect_error);<br>
}</p>
<p>// Execute query<br>
</span>$query = "SELECT id, name, email FROM users";<br>
$result = $mysqli->query($query);</p>
<p>// Check query result<br>
</span>if ($result) {<br>
// Loop through all fields in the result set<br>
while ($field = $result->fetch_field()) {<br>
echo "Field Name: " . $field->name . "<br>";<br>
echo "Field Type: " . gettype($field->type) . "<br>";<br>
echo "Max Length: " . $field->max_length . "<br><br>";<br>
}<br>
} else {<br>
echo "Query failed: " . $mysqli->error;<br>
}</p>
<p>// Close connection<br>
$mysqli->close();<br>
?><br>

4. Code Explanation

  1. Connect to the Database: Create a connection to the MySQL database using new mysqli() and check for success.

  2. Execute Query: Execute an SQL query using $mysqli->query() and store the result in the $result variable.

  3. Retrieve Field Information: Use the fetch_field() method to get detailed information about each field, including its name, type, and maximum length. Note that fetch_field() returns information for the current field each time it is called until no more fields are available.

  4. Output Field Information: Use echo to display the field’s name, type, and maximum length.

5. Field Type Analysis

The type of a field can be obtained from the type property of the object returned by fetch_field. Common MySQL field types include:

  • MYSQLI_TYPE_STRING: Represents a string type (e.g., VARCHAR).

  • MYSQLI_TYPE_INT: Represents an integer type (e.g., INT).

  • MYSQLI_TYPE_FLOAT: Represents a floating-point type (e.g., FLOAT).

  • MYSQLI_TYPE_DATE: Represents a date type (e.g., DATE).

6. Conclusion

mysqli_result::fetch_field is a highly useful tool that helps developers dynamically retrieve detailed field information when handling database query results. It is particularly valuable when dealing with complex or dynamic query result structures. After reading this article, you should have a clear understanding of how to use fetch_field and can apply this function flexibly in your projects.