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

In PHP, I use PDO with pgSQL drivers. I wanted to know how to get the value of the β€œRETURN” clause given in the INSERT SQL query. My current code is as follows:

$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 "user_id" if the insert was successful. Can you guys give me a pointer on how to do this? Thanks.

+6
php insert pdo postgresql
source share
2 answers

You tried to process the command as a return value by running

 $ret=$queryHandle->fetchAll(); 
+9
source share
 $ret = $queryHandle->fetchColumn(); 

Will return a single value instead of an array.

+11
source share

All Articles