PDO :: FETCH_COLUMN不获取任何内容 [英] PDO::FETCH_COLUMN fetches nothing

查看:114
本文介绍了PDO :: FETCH_COLUMN不获取任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码为我提供了一个包含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不获取任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆