PHP PDO-为什么LAST_INSERT_ID上的FETCH_ASSO结果没有用列名索引?

问题描述

| 所有, 我有一些PHP PDO代码,如下所示:
<?PHP
 ...
 $query=$dbh->prepare(\"SELECT LAST_INSERT_ID()\");
 $query->execute();
 $result=$query->fetch(PDO::FETCH_ASSOC); // returns an array indexed by column name as returned in result set - here column name is \"userID\" in the DB
 print_r($result);
 ...
当我运行此命令时,
print_r($result);
的结果类似于
Array([LAST_INSERT_ID()]=>18)
。为什么数组在
[LAST_INSERT_ID()]
(而不是
userID
)上建立索引,ѭ4是数据表中我列的名称PHP手册说:   PDO :: FETCH_ASSOC:返回一个在结果集中返回的按列名索引的数组 我想念什么? 谢谢, JDelage     

解决方法

        因为您没有选择该列,而是该MySQL函数的结果。 你可以用...
SELECT LAST_INSERT_ID() AS `userID`
    ,        您也可以改用PDO :: LastInsertId()