W3docs

fetch_field_direct

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

The mysqli_fetch_field_direct() function fetches the metadata of a single column in a result set, selected directly by its numeric position. Metadata describes the column itself — its name, the table it came from, its data type, length, and flags — rather than the row values stored in it. This page covers the syntax, the object it returns, a complete example, and when to reach for it instead of the related functions.

Syntax

mysqli_fetch_field_direct(mysqli_result $result, int $index): object|false

In object-oriented style the same call is $result->fetch_field_direct($index).

  • $result — a mysqli_result object returned by a query such as mysqli_query() (or mysqli_store_result() / mysqli_use_result()).
  • $index — the zero-based position of the column you want. The first column is 0, the second is 1, and so on. An out-of-range index makes the function return false.

The return value is an object whose properties describe the column. If the index is invalid, the function returns false.

The field object

The object returned exposes the following properties:

PropertyDescription
nameThe name of the column (or its alias, if one was used).
orgnameThe original column name if an alias was set.
tableThe name of the table the column belongs to (or its alias).
orgtableThe original table name if an alias was set.
defThe default value of the column, as a string.
max_lengthThe maximum width of the column for the current result set.
lengthThe declared width of the column, as defined in the table schema.
decimalsThe number of decimals for numeric columns.
typeAn integer constant identifying the data type (see below).
flagsAn integer bit-field of column flags (e.g. NOT NULL, primary key).

How to use mysqli_fetch_field_direct()

Pass a valid result set and the index of the column whose metadata you need:

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

if (!$mysqli) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);

if ($result) {
    // Metadata for the second column (index 1, zero-based)
    $field = mysqli_fetch_field_direct($result, 1);

    printf("Name:   %s\n", $field->name);
    printf("Table:  %s\n", $field->table);
    printf("Type:   %d\n", $field->type);   // integer type constant
    printf("Length: %d\n", $field->length);

    mysqli_free_result($result);
} else {
    echo "Query failed: " . mysqli_error($mysqli);
}

mysqli_close($mysqli);
?>

Here we connect with mysqli_connect() and guard against a failed connection. We run a query with mysqli_query() and, if it succeeds, call mysqli_fetch_field_direct($result, 1) to read the metadata of the second column (remember the index is zero-based). We print a few of its properties and free the result set when finished. Because type is an integer constant rather than a readable name, we format it with %d.

Reading the type constant

The type property is one of the MYSQLI_TYPE_* constants — an integer, not a word like "varchar". To turn it into something readable, map the constants yourself:

<?php
$types = [
    MYSQLI_TYPE_DECIMAL   => 'DECIMAL',
    MYSQLI_TYPE_LONG      => 'INT',
    MYSQLI_TYPE_VAR_STRING => 'VARCHAR',
    MYSQLI_TYPE_STRING    => 'CHAR',
    MYSQLI_TYPE_DATETIME  => 'DATETIME',
];

$field = mysqli_fetch_field_direct($result, 0);
echo $types[$field->type] ?? "Unknown ({$field->type})";
?>

When to use it

Reach for mysqli_fetch_field_direct() when you already know which column you want and can address it by index — for example, building a generic table renderer or validating that the second column really is the type you expect.

Conclusion

mysqli_fetch_field_direct() gives you the metadata of one column addressed directly by its zero-based index, returning an object with the column's name, table, type, length, and flags — or false for an out-of-range index. It pairs naturally with the other mysqli_fetch_field* functions when you need to inspect the shape of a result set rather than its rows.

Practice

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