PDO::FETCH_COLUMN fetches nothing

我们两清 提交于 2019-12-25 04:13:44

问题


The following code gives me an array $a containing description.

$stmt = $dbh->prepare("SELECT description, id
    FROM money_items");
$stmt->execute();
$a = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
$id = $stmt->fetchAll(PDO::FETCH_COLUMN, 1);

But the $id array comes out blank.

   Array
   (
   )

If I remove the $a = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); line, the $id array comes out fine. Am I only allowed to fetch one column from the results?


回答1:


what you are looking for is

$results = $stmt->fetchAll(PDO::FETCH_ASSOC); 

This will fetch all columns and all rows to an array -

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

To answer your actual question though, I would guess that the FetchAll function called on the statement runs through all the rows, so the internal pointer in the array is at the end when you fetch the second column.



来源:https://stackoverflow.com/questions/16530071/pdofetch-column-fetches-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!