views:

180

answers:

1

In PHP, I am using PDO with the pgSQL drivers. I wanted to know how to get the value of the "RETURNING" clause given in the INSERT sql query. My current code looks like this,

$query = 'INSERT INTO "TEST" (firstname, lastname) VALUES ('John', 'Doe') RETURNING user_id';
$queryHandle = $connection->prepare($query);
$queryHandle->execute();

Obviously

$queryHandle->execute();

returns TRUE or FALSE. But I wanted to get the value of "user_id" if the insert was successful. Can you guys give me a pointer as to how to go about it? Thanks.

+1  A: 

Did you tried to treat the command as a select returning, running

$ret=$queryHandle->fetchAll();
Pentium10
It definitely works! :) But I was hoping for a more direct/clean way to do this. Maybe I am asking for too much...
Chantz
There is no cleaner way, as you have a returned data. It is trivial you have to fetch it.
Pentium10
k. Just a minor thing, I used fetch() instead of fetchAll() sine its guaranteed that there be only one insert at a time.
Chantz