简体   繁体   English

PHP PDO:fetchAll(PDO :: FETCH_COLUMN | PDO :: FETCH_GROUP)不起作用

[英]PHP PDO: fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP) doesn't work

I have a question about PDO in PHP: 我对PHP中的PDO有疑问:

I'm trying to fetch a column from my mysql database, indexed by the id of each row... Now php.net has an example on how to do it: http://www.php.net/manual/en/pdostatement.fetchall.php (example #3) 我正试图从我的mysql数据库中获取一列,并按每一行的ID索引...现在php.net上有一个有关如何执行此操作的示例: http : //www.php.net/manual/zh/ pdostatement.fetchall.php (示例#3)

    <?php
$insert = $dbh->prepare("INSERT INTO fruit(name, colour) VALUES (?, ?)");
$insert->execute(array('apple', 'green'));
$insert->execute(array('pear', 'yellow'));

$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Group values by the first column */
var_dump($sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP));
?>

But this doesn't seem to work for me... All i get is: 但这似乎对我不起作用...我得到的是:

array {

   [id1] => null;
   [id2] => null;
   [id3] => null;
   ...

}

Even when i copy the exact same code, and only edit the table name etc. Does anyone know what can cause this problem? 即使当我复制完全相同的代码,并且仅编辑表名等时,没有人知道会导致此问题的原因吗?

As you can see from the output in the example you linked to, it doesn't work the way you expect. 从链接到的示例的输出中可以看到,它无法按预期的方式工作。

So, just loop over result using while fetch, storing resul in array manually and set whatever index you like. 因此,只需使用获取时循环结果,将resul手动存储在数组中并设置所需的索引即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM