SoFunction
Updated on 2025-04-09

PHP PDOStatement::fetchColumn explanation

PDOStatement::fetchColumn

PDOStatement::fetchColumn — Returns a separate column from the next row in the result set. (PHP 5 >= 5.1.0, PECL pdo >= 0.9.0)

illustrate

grammar

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

Returns a separate column from the next row in the result set, and if it is gone, returns FALSE .

parameter

column_number

  • The index number of the column you want to retrieve from the row (index starting with 0). If no value is provided, PDOStatement::fetchColumn() gets the first column.

Return value

PDOStatement::fetchColumn()Return a separate column from the next row in the result set.

Note: If you use PDOStatement::fetchColumn() to retrieve data, there is no way to return another column of the same row. **

Example

Return to the first column of the next row

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Get the first column from the next row in the result set */
print("Get the first column from the next row in the result set:\n");
$result = $sth->fetchColumn();
print("name = $result\n");
print("Get the second column from the next row in the result set:\n");
$result = $sth->fetchColumn(1);
print("colour = $result\n");
?>

The above examples will output:

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

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below