PHP mysqli_fetch_field_direct() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_fetch_field_direct() returns metadata for one column in a mysqli_result, chosen by a zero-based index.

Unlike mysqli_fetch_field(), it does not walk a cursor—you name the column number you want. An invalid index returns false. The object style is $result->fetch_field_direct($index).

Syntax

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

PHP
$field = mysqli_fetch_field_direct($result, 0);
CallWhat it does
mysqli_fetch_field_direct($result, 0)Returns metadata for the first column.
mysqli_fetch_field_direct($result, 1)Returns metadata for the second column.
mysqli_fetch_field_direct($result, 99)Returns false when the index is out of range.

Use this when you already know the column position. For a sequential walk, mysqli_fetch_field() is simpler. The returned object has the same style of properties as fetch_field().

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

Parameters

ParameterDescription
resultRequired. The mysqli_result that holds the column definitions.
indexRequired. Zero-based field number to inspect.

Return Value

Return ValueDescription
object|falseA field metadata object for that index, or false if the index is invalid.

PHP Examples

Example 1 — Get Metadata for Column Zero

Read the first field definition directly and print its name from a sample query.

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

echo $field->name, PHP_EOL;

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

How It Works

  1. The result has id at index 0 and name at index 1.
  2. mysqli_fetch_field_direct($result, 0) targets the first column.
  3. The name property prints as id.
  4. No field cursor move is needed because the index is explicit.

Example 2 — Read the Second Column Directly

Skip the cursor and ask for index 1 to get the name column metadata.

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

echo $field->name, " / ", $field->table, PHP_EOL;

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

How It Works

  1. Index 1 is the second selected column.
  2. The metadata object describes the name field.
  3. table shows the sample source table products.
  4. You can call any valid index without fetching earlier fields first.

Example 3 — Invalid Index Returns False

Ask for a field number that does not exist and dump the false result.

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

var_dump($field);

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

How It Works

  1. The result has only one column at index 0.
  2. Index 3 is invalid.
  3. mysqli_fetch_field_direct() returns false.
  4. Check the return value before reading object properties.

Frequently Asked Questions

It returns metadata for one result column chosen by a zero-based index, or false if that index does not exist.
fetch_field() walks fields in order with an internal cursor. fetch_field_direct() jumps straight to the index you pass.
Call $result->fetch_field_direct($index) on a mysqli_result.
They start at 0. The first column in the SELECT list is index 0.
No. Pass an integer position. To find a name, loop fetch_field() or use fetch_fields() and search the array.

Next: mysqli_fetch_fields()

mysqli_fetch_fields() returns an array of field metadata objects for every column in a mys.

mysqli_fetch_fields() 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