开发者

PHP PDO - Why is the result of a FETCH_ASSO() on a LAST_INSERT_ID() not indexed with the column name?

开发者 https://www.devze.com 2023-03-12 01:20 出处:网络
All, I have some PHP PDO code that looks like this: <?php ... $query=$dbh->prepare(\"SELECT LAST_INSERT_ID()\");

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 ma开发者_StackOverflownual 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

0

精彩评论

暂无评论...
验证码 换一张
取 消