问题描述
以下代码为我提供了一个包含description
的数组$a
.
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);
但是$id
数组为空.
Array
(
)
如果我删除$a = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
行,则$id
数组会很好.我只能从结果中提取一列吗?
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?
推荐答案
您正在寻找的是
$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
尽管要回答您的实际问题,我会猜测,该语句上调用的FetchAll函数遍历所有行,因此,当您获取第二个时,数组中的内部指针位于末尾列.
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.
这篇关于PDO :: FETCH_COLUMN不获取任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!