PHP mysqli_fetch_field() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_fetch_field() returns metadata for the next field (column) in a mysqli_result as an object. Each call moves to the following field definition.

The object includes properties such as name, table, type, max_length and flags. When every field has been read it returns false. The object style is $result->fetch_field().

Syntax

Call mysqli_fetch_field() with the values you need. Start with this line.

PHP
$field = mysqli_fetch_field($result);
CallWhat it does
mysqli_fetch_field($result)Returns metadata for the next column, or false when finished.
$field->nameReads the column name from the metadata object.
while ($field = mysqli_fetch_field($result)) { … }Loops over every column definition in the result.

This reads column definitions, not row data. Use fetch_assoc() or fetch_row() for values. mysqli_field_seek() can move the field cursor before you call mysqli_fetch_field() again.

Library declarations
PHP
mysqli_fetch_field(mysqli_result $result): object|false

Parameters

ParameterDescription
resultRequired. A mysqli_result whose column metadata you want to inspect.

Return Value

Return ValueDescription
object|falseAn object describing the next field, or false when there are no more fields.

PHP Examples

Example 1 — Read the First Field Name

Fetch metadata for the first column and print its name from a sample result.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products LIMIT 1");
$field = mysqli_fetch_field($result);

echo $field->name, PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. The SELECT defines two columns: id and name.
  2. mysqli_fetch_field() returns metadata for the first column.
  3. The name property is "id" in this sample.
  4. A later call would return the name column next.

Example 2 — List Every Column Name

Loop mysqli_fetch_field() until it returns false and collect each field name.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name, price FROM products LIMIT 1");

$names = [];
while ($field = mysqli_fetch_field($result)) {
    $names[] = $field->name;
}

echo implode(", ", $names), PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. Three columns are selected.
  2. Each successful fetch_field() call adds one name to the list.
  3. false ends the loop after the last column.
  4. implode() prints the sample column list.

Example 3 — Inspect Table and Type Properties

Print a few useful metadata properties for the first field in a fixed demo.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT name FROM products LIMIT 1");
$field = mysqli_fetch_field($result);

echo "name=", $field->name, PHP_EOL;
echo "table=", $field->table, PHP_EOL;
echo "max_length=", $field->max_length, PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. mysqli_fetch_field() returns one metadata object.
  2. name is the column name from the result.
  3. table is the source table name in this sample.
  4. max_length reflects the longest value length seen for that column in the result.

Frequently Asked Questions

It returns an object with metadata for the next column in a result set, or false when every field has been read.
No. It returns column definitions such as name and type. Use fetch_assoc() or fetch_row() for data.
Use $result->fetch_field() on a mysqli_result object.
Call mysqli_field_seek($result, 0), then call mysqli_fetch_field() again.
mysqli_fetch_field() returns one field at a time. mysqli_fetch_fields() returns an array of all field objects at once.

Next: mysqli_fetch_field_direct()

mysqli_fetch_field_direct() returns metadata for one column in a mysqli_result, chosen by .

mysqli_fetch_field_direct() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful