W3docs

fetch_field

In this article, we will focus on the mysqli_fetch_field() function in PHP, which is used to fetch meta-data for a single column from a result set. We will

The mysqli_fetch_field() function returns metadata about a column — not the data inside it. Each time you call it, you get an object describing the next column in the result set: its name, data type, length, the table it came from, and assorted flags. This page explains when that is useful, how the function behaves, what every property means, and how to read it in both object-oriented and procedural styles.

What mysqli_fetch_field() does

mysqli_fetch_field() walks the columns of a result set one at a time, like a cursor. The result set keeps an internal field pointer; every successful call advances it by one. When you reach the last column, the next call returns false, which makes it convenient to drive a while loop.

It returns metadata, so you never need actual rows to use it — a query that matches zero rows still describes its columns. That is exactly why it is handy: you can inspect the shape of a result before processing it.

Syntax (both styles are equivalent):

// Object-oriented style
$fieldInfo = $result->fetch_field();

// Procedural style
$fieldInfo = mysqli_fetch_field($result);

It takes the result set as its only input and returns an object on success or false when there are no more fields.

Reading column metadata

The example below connects, runs a SELECT, and loops over the result's columns. Each iteration prints the column's name, type code, and maximum length.

How to use the mysqli_fetch_field() function?

<?php
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$query = "SELECT * FROM my_table";
$result = $mysqli->query($query);

if ($result) {
    while ($field = $result->fetch_field()) {
        printf("Name: %s\n", $field->name);
        printf("Type: %s\n", $field->type);
        printf("Length: %d\n", $field->length);
    }
} else {
    echo "Query failed: " . $mysqli->error;
}

$result->free();
$mysqli->close();
?>

The while ($field = $result->fetch_field()) loop ends naturally when fetch_field() returns false after the last column. Because the field pointer advances each call, the first iteration describes the first column, the second iteration the second column, and so on.

The field object and its properties

Each call returns a stdClass-like object. The most commonly used properties are:

PropertyMeaning
nameThe column's name as returned by the query (an alias, if used)
orgnameThe original column name, before aliasing
tableThe table the column belongs to (an alias, if used)
orgtableThe original table name
typeThe data type, as an integer constant (see below)
lengthThe declared width of the column
max_lengthThe maximum width of the actual values (often 0 unless buffered)
flagsA bitmask of column flags (NOT_NULL, PRI_KEY, …)
decimalsNumber of decimals for numeric fields

The type property is an integer, not a readable string. PHP exposes constants such as MYSQLI_TYPE_VARCHAR, MYSQLI_TYPE_LONG (an integer column), and MYSQLI_TYPE_DATETIME so you can compare against it:

if ($field->type === MYSQLI_TYPE_LONG) {
    echo "{$field->name} is an integer column\n";
}

Targeting one specific column

fetch_field() is sequential, so to inspect, say, only the third column you would either loop until you reach it or seek to it first. The field pointer can be repositioned with field_seek(), and a single column can be fetched directly by index with fetch_field_direct():

// Jump to column index 2 (the third column), then read it
$result->field_seek(2);
$field = $result->fetch_field();
echo $field->name;

When to use it (and the alternatives)

Reach for fetch_field() when you want to iterate over columns and react to each one — for example, to auto-generate a table header, build a form, or decide how to format each value by type.

  • If you need every column's metadata in one go, fetch_fields() returns them all as an array in a single call, which is usually cleaner than a loop.
  • If you only need how many columns there are, use field_count.
  • To read the actual row data rather than column metadata, use fetch_assoc() or fetch_array().

Conclusion

mysqli_fetch_field() gives you a column-by-column view of a result set's structure: names, types, lengths, and flags, advancing through the columns one call at a time. Loop over it to introspect a query before you process its rows, combine it with field_seek() to target a column, or switch to fetch_fields() when you want all the metadata at once.

Practice

Practice
What does the fetch_field() function in PHP do?
What does the fetch_field() function in PHP do?
Was this page helpful?