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

All,

I have some PHP PDO code that looks like this:

<?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);
 ...

When I run this, the result of print_r($result); is something like Array([LAST_INSERT_ID()]=>18). Why is the array indexed on [LAST_INSERT_ID()] as opposed to userID, which is the name of my column in the data table? The PHP manual says:

PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set

What am I missing?

Thanks,

JDelage

Because you are not selecting the column, but the result of that MySQL function.

You could use...

SELECT LAST_INSERT_ID() AS `userID`

You could also use PDO::LastInsertId() instead