PHP 7.0.0 RC 8 Released


(PHP 5 >= 5.1.0, PECL pdo >= 0.9.0)

PDOStatement::fetchColumn Returns a single column from the next row of a result set


public string PDOStatement::fetchColumn ([ int $column_number = 0 ] )

Returns a single column from the next row of a result set or FALSE if there are no more rows.

Elenco dei parametri


0-indexed number of the column you wish to retrieve from the row. If no value is supplied, PDOStatement::fetchColumn() fetches the first column.

Valori restituiti

PDOStatement::fetchColumn() returns a single column in the next row of a result set.


There is no way to return another column from the same row if you use PDOStatement::fetchColumn() to retrieve data.


Example #1 Return first column of the next row

$dbh->prepare("SELECT name, colour FROM fruit");

/* Fetch the first column from the next row in the result set */
print("Fetch the first column from the next row in the result set:\n");
$result $sth->fetchColumn();
"name = $result\n");

"Fetch the second column from the next row in the result set:\n");
$result $sth->fetchColumn(1);
"colour = $result\n");

Il precedente esempio visualizzerĂ :

Fetch the first column from the next row in the result set:
name = lemon
Fetch the second column from the next row in the result set:
colour = red

Vedere anche:

add a note add a note

User Contributed Notes 5 notes

PhoneixSegovia at GOOGLE_MAIL_SERVER dot com
5 years ago
fetchColumn return boolean false when a row not is found or don't had more rows.
seanferd at assmasterdonkeyranch dot com
8 years ago
This is an excellent method for returning a column count. For example:

= new PDO('mysql:host=localhost;dbname=pictures','user','password');
$pics = $db->query('SELECT COUNT(id) FROM pics');
$this->totalpics = $pics->fetchColumn();
$db = null;
In my case $pics->fetchColumn() returns 641 because that is how many pictures I have in my db.
ohcc at 163 dot com
2 days ago
usernote #118367 is just a mistake.

I have taken a misunderstanding about PDOStatement::fetch() with PDO::FETCH_COLUMN.

Sorry, it's my bad.
ohcc at 163 dot com
2 days ago
When an invalid column offset is suplied, PDOStatement::fetch() with PDO::FETCH_COLUMN always returns the value of the first column of the first row.

However, PDOStatement::fetchAll() with PDO::FETCH_COLUMN and an invalid column offset whill throw an exception instead.

= new PDO(....);// let's assume the database is connected.

$PDOStatement = $pdo->query('SELECT * FROM `WuXiancheng`');
$PDOStatement->fetch(PDO::FETCH_COLUMN, -1); // minus column offset

$PDOStatement = $pdo->query('SELECT * FROM `WuXiancheng`');
$PDOStatement->fetch(PDO::FETCH_COLUMN, 3691); // column offset is number greater than column count

$PDOStatement = $pdo->query('SELECT * FROM `WuXiancheng`');
$PDOStatement->fetch(PDO::FETCH_COLUMN, 36.5); // column offset is a float number


Examples above will all input the first column value of the first row of data get from the database.

This is really weird.
gaiusgracchus33 at gmail dot com
5 months ago
It looks like the example may be saying it is fetching the second column from the NEXT row, not the same one that returned 'lemon' for name.   The warning message says you can't return another column from the SAME row after fetchColumn().
To Top