Add fetch_column method to mysqli View as Markdown

Implemented (PHP 8.1)
Authors
Kamil Tekiela, dharman@php.net
Date
2021-03-28
Version
1.0
Target
PHP 8.1
Implementation
https://github.com/php/php-src/pull/6798
Source
wiki.php.net/rfc/mysqli_fetch_column

Vote

Add mysqli_fetch_column to mysqli? closed
Yes
18
No
2
20 voters
alec No 2021-04-21 12:31
asgrim Yes 2021-04-24 05:48
ashnazg Yes 2021-04-21 14:23
bwoebi Yes 2021-04-25 20:56
dharman Yes 2021-04-17 16:55
ehrhardt Yes 2021-04-21 18:59
galvao Yes 2021-04-22 08:27
geekcom Yes 2021-04-28 20:12
girgias Yes 2021-04-19 13:31
kalle Yes 2021-04-18 09:34
kguest Yes 2021-04-19 11:55
kinncj Yes 2021-04-22 21:43
kocsismate Yes 2021-04-17 17:09
nicolasgrekas Yes 2021-04-19 15:23
nikic Yes 2021-04-19 09:33
ocramius No 2021-04-19 16:07
sammyk Yes 2021-04-24 19:33
santiagolizardo Yes 2021-04-23 17:14
sergey Yes 2021-04-27 07:34
trowski Yes 2021-04-26 16:12

Discussions on the mailing list

[RFC] Add fetch_column method to mysqli
by Kamil Tekiela · 4 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:50 UTC

Introduction

There are 3 methods in mysqli that return arrays from result set, one that returns stdClass object, and one that returns an array of arrays. However, mysqli doesn't have a method to return a scalar value.

Proposal

As of now, there is no easy way to fetch a scalar value in mysqli. If the SQL statement can return no rows then the simplest way to fetch scalars is to fetch an array and using the null-coalescing operator default the value to false. This is due to all the fetch methods returning false if no more rows are present in the result set.

$result = $mysqli->query('SELECT username FROM users WHERE id = 123');
echo $result->fetch_row()[0] ?? false;

This RFC borrows the idea from PDO to add another method to mysqli_result class. The method would be called fetch_column to keep with the existing mysqli naming convention.

The above example can then be simplified to a single method call:

$result = $mysqli->query('SELECT username FROM users WHERE id = 123');
echo $result->fetch_column();

The new method will also accept an optional integer parameter to specify which column to fetch from the current row. The index is 0-based just like in PDO.

Just like with other fetch_* methods this one will also move the internal result pointer to the next row when called.

Difference between PDO and mysqli

There will be only two differences from PDO: the name of the method, and the fact that MySQL doesn't have boolean types thus this method can never return a boolean. However, the method can still return false, which indicates that no row could be fetched from the result.

Backward Incompatible Changes

None.

Proposed PHP Version(s)

Next PHP 8.X (8.1)

RFC Impact

This RFC only adds a new method to an existing class. It is just syntactic sugar to avoid the null-coalescing operator.

New Constants

None

php.ini Defaults

None.

Future Scope

It would be nice to have similar functionality available with mysqli_result::fetch_all(). This is not included in the current proposal due to the fact that it would require rewriting that method to support the MYSQLI_CLASS constant as well. This is certainly possible but would be much more work.

Of course, it is very simple to fetch single column values into an array with the current functionality:

$result = $mysqli->query('SELECT username FROM users');
$usernames = [];
foreach ($result as ['username' => $usernames[]]);

Proposed Voting Choices

Simple yes/no vote.

Voting started on 2021-04-17 17:00:00Z and ends on 2021-05-01 17:00:00Z

Vote: Add mysqli_fetch_column to mysqli? — Yes: 18 · No: 2 (closed)

Implementation

PR here

References

https://externals.io/message/113832#113832