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|falseIn object-oriented style the same call is $result->fetch_field_direct($index).
$result— amysqli_resultobject returned by a query such asmysqli_query()(ormysqli_store_result()/mysqli_use_result()).$index— the zero-based position of the column you want. The first column is0, the second is1, and so on. An out-of-range index makes the function returnfalse.
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:
| Property | Description |
|---|---|
name | The name of the column (or its alias, if one was used). |
orgname | The original column name if an alias was set. |
table | The name of the table the column belongs to (or its alias). |
orgtable | The original table name if an alias was set. |
def | The default value of the column, as a string. |
max_length | The maximum width of the column for the current result set. |
length | The declared width of the column, as defined in the table schema. |
decimals | The number of decimals for numeric columns. |
type | An integer constant identifying the data type (see below). |
flags | An 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.
- To walk over every column's metadata one at a time, use
mysqli_fetch_field(), which advances an internal pointer with each call. - To grab the metadata for all columns at once as an array, use
mysqli_fetch_fields(). - To fetch the row data rather than column metadata, use
mysqli_fetch_assoc(),mysqli_fetch_row(), ormysqli_fetch_object(). - To find out how many columns the result has before indexing into them, use
mysqli_field_count().
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.