Search code examples
phpinsertpdopostgresql

In PHP, when using PDO with pgSQL how to get the value of "RETURNING" clause in the original INSERT sql query


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.


Solution

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

    $ret=$queryHandle->fetchAll();