PHP PDO:检索单列结果的方法之间的差异

时间:2022-05-10 13:25:16

What's the difference between

有什么区别

$PDOStatement->fetchColumn();

and

$PDOStatement->fetch(PDO::FETCH_COLUMN);

(if one exists)? Or are they functionally similar but only aesthetically different?

(如果存在的话)?或者它们功能相似但只是在美学上不同?

3 个解决方案

#1


By default fetchColumn() will return only 'value' while other by default will return array('column_name'=>'value'). You'd have to use setFetchMode() to change that.

默认情况下,fetchColumn()将仅返回'value',而其他默认情况下将返回数组('column_name'=>'value')。你必须使用setFetchMode()来改变它。

$PDOStatement->fetchColumn($colno);

would be equivalent to:

相当于:

$PDOStatement->setFetchMode(PDO::FETCH_COLUMN, $colno);
$PDOStatement->fetch();

#2


From the doc here for fetch, there doesn't seem to be a PDO::FETCH_COLUMN style. If that's true, then the difference is fetch will return a row, while fetchColumn will only return the specified column.

从这里的文档获取,似乎没有PDO :: FETCH_COLUMN样式。如果这是真的,那么差异是fetch将返回一行,而fetchColumn将只返回指定的列。

#3


By default fetchColumn() will return only 'value' while other by default will return array('column_name'=>'value'). You'd have to use setFetchMode() to change that.

默认情况下,fetchColumn()将仅返回'value',而其他默认情况下将返回数组('column_name'=>'value')。你必须使用setFetchMode()来改变它。

#1


By default fetchColumn() will return only 'value' while other by default will return array('column_name'=>'value'). You'd have to use setFetchMode() to change that.

默认情况下,fetchColumn()将仅返回'value',而其他默认情况下将返回数组('column_name'=>'value')。你必须使用setFetchMode()来改变它。

$PDOStatement->fetchColumn($colno);

would be equivalent to:

相当于:

$PDOStatement->setFetchMode(PDO::FETCH_COLUMN, $colno);
$PDOStatement->fetch();

#2


From the doc here for fetch, there doesn't seem to be a PDO::FETCH_COLUMN style. If that's true, then the difference is fetch will return a row, while fetchColumn will only return the specified column.

从这里的文档获取,似乎没有PDO :: FETCH_COLUMN样式。如果这是真的,那么差异是fetch将返回一行,而fetchColumn将只返回指定的列。

#3


By default fetchColumn() will return only 'value' while other by default will return array('column_name'=>'value'). You'd have to use setFetchMode() to change that.

默认情况下,fetchColumn()将仅返回'value',而其他默认情况下将返回数组('column_name'=>'value')。你必须使用setFetchMode()来改变它。