PDO :: FETCH_ASSOC不返回正确的表列 [英] PDO::FETCH_ASSOC Does not return the correct table columns

查看:95
本文介绍了PDO :: FETCH_ASSOC不返回正确的表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,目前正在开发一个小型Web应用程序.下面是使用PDO进行数据库查询的测试代码:

I'm new to PHP and I am currently developing a small web application. Below is the test code for the DB query using PDO:

try 
{
    $dsn = "mysql:host=localhost;dbname=auction";
    $opt = array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
    );
    $pdo = new PDO($dsn, 'root', 'password', $opt);
}

catch(PDOException $e)
{
    echo $e->getMessage();
}

$stmt = $pdo->query("SELECT id,username,password,name,level FROM users");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$test = $stmt->fetchALL();
print_r($test);

所述代码的输出如下:

Array ( [0] => Array ( [id] => 1 [username] => admin [users] => Administrator [auction] => 0 ) 
        [1] => Array ( [id] => 2 [username] => jodel [users] => Jodel Ross [auction] => 1 ) ) 

我很困惑,因为从我的角度来看,上面的代码应该给出一个关联数组,其索引是返回结果集的列,即id,username,password,name,level.但事实并非如此.

I am confused because, from my point of view, the above code should have given an associative array whose index are the columns of the returned result set, i.e. id, username,password,name,level. But it is not so.

此外,如果我使用PDO :: FETCH_NUM,则正确返回正确的字段数和相应的数据,如下所示:

Moreover, If I used PDO::FETCH_NUM, the correct number of fields and corresponding data are returned correctly as show below:

Array ( [0] => Array ( [0] => 1 
                       [1] => admin 
                       [2] => 21232f297a57a5a743894a0e4a801fc3 
                       [3] => Administrator 
                       [4] => 0 )
        [1] => Array ( [0] => 2 
                       [1] => jodel_ross 
                       [2] => 2cdaeb5df4cf941d9c5650591cba1fdc 
                       [3] => Jodel Ross, Jr. 
                       [4] => 1 ) ) 

请帮助,我需要了解原因,并且我已经在网上搜索了答案,但没有找到答案. 谢谢.

Please help, I need to understand why and I have search the web for answers and have not found any. Thanks.

Development environment:
Windows 7 x64
PHP 5.3.6 
Mysql 5.5 
Apache 2.0.64 
Zend Framework 1.11

推荐答案

Zend Framework 1.x,PHP 5.3.x和setFetchMode()仍然存在打开错误,这就是为什么它无法按预期工作的原因

There is still an open error with Zend Framework 1.x, PHP 5.3.x and setFetchMode() and that's the reason why it doesn't work as expected.

请参见 http://framework.zend.com/issues/browse/ZF-10866 http://framework.zend.com/issues/browse/ZF- 3470

请为关闭此错误投票或提交补丁.

Please vote for closing this error or submit a patch.

这篇关于PDO :: FETCH_ASSOC不返回正确的表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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