Example 1 — List All Column Names
Fetch every field object and print the column names from a sample SELECT.
<?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
- mysqli_fetch_fields() returns three metadata objects.
- The foreach loop visits each object in order.
- Each name property is printed on its own line.
- Row fetching is not required to read column names.

