PHP mysqli_fetch_column() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_fetch_column() reads the next row from a mysqli_result and returns a single column value from that row. It was added in PHP 8.1.

The default column index is 0 (the first column). When there are no more rows it returns null. The object style is $result->fetch_column($column).

Syntax

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

PHP
$name = mysqli_fetch_column($result, 1);
CallWhat it does
mysqli_fetch_column($result)Returns the first column of the next row.
mysqli_fetch_column($result, 1)Returns the second column (index 1) of the next row.
while (($id = mysqli_fetch_column($result)) !== null) { … }Loops over a single-column result until null.

mysqli_fetch_column() needs PHP 8.1 or later. It advances the row cursor just like other fetch functions, but returns one scalar instead of a full row array. Out-of-range column indexes return false.

Library declarations
PHP
mysqli_fetch_column(mysqli_result $result, int $column = 0): null|int|float|string|false

Parameters

ParameterDescription
resultRequired. A mysqli_result from a SELECT (or similar) query.
columnOptional. Zero-based column index to return. The default is 0.

Return Value

Return ValueDescription
null|int|float|string|falseThe column value for the next row, null when finished, or false if the column index is invalid.

PHP Examples

Example 1 — Read the First Column of Each Row

Fetch product names from a single-column SELECT. Sample names are fixed for the tutorial.

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

while (($name = mysqli_fetch_column($result)) !== null) {
    echo $name, PHP_EOL;
}

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

How It Works

  1. The SELECT returns only the name column.
  2. mysqli_fetch_column() with no second argument reads column 0.
  3. The loop continues until the function returns null.
  4. Three sample product names are printed.

Example 2 — Pick a Later Column by Index

Pass column index 1 to read the second field of each row from a two-column query.

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

echo mysqli_fetch_column($result, 1), PHP_EOL;
echo mysqli_fetch_column($result, 1), PHP_EOL;

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

How It Works

  1. Each row has id at index 0 and name at index 1.
  2. mysqli_fetch_column($result, 1) returns only the name.
  3. The second call advances to the next row and reads name again.
  4. The id values are skipped because we asked for column 1.

Example 3 — Invalid Column Index Returns False

Ask for a column that does not exist and check the false result carefully.

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

$value = mysqli_fetch_column($result, 5);
var_dump($value);

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

How It Works

  1. The result has only one column, index 0.
  2. Index 5 is out of range.
  3. mysqli_fetch_column() returns false for an invalid index.
  4. Do not confuse false (bad index) with null (no more rows).

Frequently Asked Questions

It returns one column value from the next result row. The default column is index 0. It was added in PHP 8.1.
PHP 8.1. On older versions use mysqli_fetch_row() or mysqli_fetch_assoc() and read the field from the array.
Call $result->fetch_column($column) on a mysqli_result object.
null means there is no next row. false usually means the column index was invalid. Use !== null when looping.
No. mysqli_fetch_column() only accepts a zero-based integer index. For names, use mysqli_fetch_assoc().

Next: mysqli_fetch_field()

mysqli_fetch_field() returns metadata for the next field (column) in a mysqli_result as an.

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