PHP mysqli_fetch_fields() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_fetch_fields() returns an array of field metadata objects for every column in a mysqli_result.

It is a one-shot alternative to looping mysqli_fetch_field(). Each array item has properties such as name, table and type. The object style is $result->fetch_fields().

Syntax

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

PHP
$fields = mysqli_fetch_fields($result);
CallWhat it does
mysqli_fetch_fields($result)Returns an array of metadata objects, one per column.
$fields[0]->nameReads the name of the first column from the array.
count(mysqli_fetch_fields($result))Counts how many columns the result has.

Use mysqli_fetch_fields() when you want all column definitions at once for headers, exporters or debugging. It does not consume row data, so you can still fetch_assoc() afterward.

Library declarations
PHP
mysqli_fetch_fields(mysqli_result $result): array

Parameters

ParameterDescription
resultRequired. The mysqli_result whose columns you want to describe.

Return Value

Return ValueDescription
arrayAn array of field metadata objects in column order.

PHP Examples

Example 1 — List All Column Names

Fetch every field object and print the column names from a sample SELECT.

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

foreach ($fields as $field) {
    echo $field->name, PHP_EOL;
}

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

How It Works

  1. mysqli_fetch_fields() returns three metadata objects.
  2. The foreach loop visits each object in order.
  3. Each name property is printed on its own line.
  4. Row fetching is not required to read column names.

Example 2 — Count Columns from Metadata

Use count() on the fields array as another way to learn the result width.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, email FROM users LIMIT 1");
$fields = mysqli_fetch_fields($result);

echo count($fields), PHP_EOL;

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

How It Works

  1. The SELECT defines two columns.
  2. mysqli_fetch_fields() builds an array with two objects.
  3. count() returns 2 for this sample.
  4. mysqli_field_count() on the connection is another way to get the same number after a query.

Example 3 — Build a Simple Header Line

Join field names into a CSV-style header using the metadata array.

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

$headers = array_map(fn($f) => $f->name, $fields);
echo implode(",", $headers), PHP_EOL;

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

How It Works

  1. array_map() pulls the name property from each field object.
  2. implode() joins the names with commas.
  3. The sample header matches the SELECT list.
  4. You can later append data rows with fetch_assoc().

Frequently Asked Questions

It returns an array of metadata objects describing every column in the result set.
fetch_fields() returns all columns at once. fetch_field() returns one column per call and advances an internal field cursor.
Use $result->fetch_fields() on a mysqli_result object.
No. It only reads column definitions. You can still fetch row data afterward.
Common ones include name, orgname, table, orgtable, max_length, length, type, and flags. Exact availability can depend on the driver.

Next: mysqli_fetch_lengths()

mysqli_fetch_lengths() returns an array of byte lengths for each column in the current row.

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