fetch_fields
In this article, we will focus on the mysqli_fetch_fields() function in PHP, which is used to fetch an array of field objects representing the columns in a
The mysqli_fetch_fields() function returns an array of objects that describe the columns (fields) of a MySQLi result set — their names, types, lengths, source tables, and flags. It reads metadata about the columns, not the row data itself. This page explains the syntax, what each field object contains, when you would reach for it, and the common gotchas.
Syntax
mysqli_fetch_fields(mysqli_result $result): arrayIn the object-oriented style the same call is $result->fetch_fields().
$result— a result set returned bymysqli_query(),mysqli_store_result(), ormysqli_use_result().- Return value — an array of
stdClassobjects, one per column, in the order the columns appear in the result. Each object exposes properties such asname,orgname,table,orgtable,type,length,max_length,decimals, andflags.
What each field object contains
| Property | Description |
|---|---|
name | The column name (or its alias, if you used AS). |
orgname | The original column name, ignoring any alias. |
table | The table name (or its alias). |
orgtable | The original table name. |
max_length | The width of the widest value in the result (only set after mysqli_store_result()). |
length | The defined width of the column as declared in the schema. |
type | An integer type code (see the MYSQLI_TYPE_* constants). |
decimals | Number of decimals for numeric fields. |
flags | A bitmask of MYSQLI_*_FLAG flags such as NOT_NULL, PRI_KEY, AUTO_INCREMENT. |
When would I use it?
Reach for mysqli_fetch_fields() when you need to work with a query result generically — without knowing the columns ahead of time. Typical cases:
- Building an admin/data-grid that renders any
SELECT *query, using column names for table headers. - Exporting results to CSV with a header row.
- Inspecting whether a column is a primary key or auto-increment before generating an edit form.
If you only need the column names, this is the most direct way to get them. To pull one field's description at a time, use mysqli_fetch_field() instead, and to count the columns use mysqli_field_count().
Example: list every column in a result set
<?php
$mysqli = mysqli_connect("localhost", "user", "password", "shop");
$result = mysqli_query($mysqli, "SELECT id, name, price FROM products");
$fields = mysqli_fetch_fields($result);
foreach ($fields as $field) {
printf("Name: %s, Type: %d, Length: %d\n",
$field->name, $field->type, $field->length);
}
mysqli_free_result($result);
mysqli_close($mysqli);
?>The foreach loop walks the array of field objects and prints the name, type code, and defined length of each column. For the three selected columns the output looks like:
Name: id, Type: 3, Length: 11
Name: name, Type: 253, Length: 255
Name: price, Type: 246, Length: 10The numeric type values come from the MYSQLI_TYPE_* constants — for example 3 is MYSQLI_TYPE_LONG (an INT) and 253 is MYSQLI_TYPE_VAR_STRING (a VARCHAR).
Building a CSV header from the field names
Because the field array is generic, you can build a header row for any query without hard-coding column names:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM products");
$header = array_map(
fn($field) => $field->name,
mysqli_fetch_fields($result)
);
echo implode(",", $header), "\n"; // id,name,pricearray_map() turns the array of field objects into a plain array of names, and implode() joins them with commas.
Gotchas
mysqli_fetch_fields()is metadata only. It does not move the row cursor and does not return any row data — combine it withmysqli_fetch_assoc()ormysqli_fetch_array()to read the actual rows.max_lengthis0with unbuffered results. It is only populated when the full result set is buffered on the client viamysqli_store_result()(whichmysqli_query()uses by default). Withmysqli_use_result()the value stays0.typeandflagsare integers, not strings. Compare them against theMYSQLI_TYPE_*andMYSQLI_*_FLAGconstants rather than magic numbers, so the intent is clear.- Always check that
$resultis truthy before calling the function — a failed query returnsfalse, and passingfalseraises an error.
Related functions
mysqli_fetch_field()— fetch one column's metadata at a time.mysqli_field_count()— count the columns in the result.mysqli_field_seek()— move the field cursor to a specific column.mysqli_fetch_assoc()— read result rows as associative arrays.