PHP, Postgres will help with RETURNING

I think I understand how PostgreSQL and RETURNING work — I found many, many resources. If I catch, it will look something like

"INSERT INTO table (column2, column3) VALUES ('value1', 'value2') RETURNING id;" 

However, I cannot find anything to help me access this through PHP. When I thought I understood, I tried

 $new_id = pg_query($dbc, "INSERT INTO table (column2, column3) ". "VALUES ('value1', 'value2') RETURNING id;"); return $new_id; 

But it returns NULL. I also tried to execute and declare the variable separately with one request. After you found the clock to solve, I settled on the SELECT (id) SELECT statement / function, but that still bothers me. Any help is appreciated.

I am using Postgres 8.4 and PHP 5.3.

+7
source share
1 answer

$new_id does not contain an identifier, but it is a resource descriptor. You need to extract data from it, since the query will be SELECT, with an example pg_fetch_array($new_id) .

RETURNING PostgreSQL clause designs any fields of inserted or modified rows, i.e. INSERT|UPDATE … RETURNING id, field1, field2 .

+12
source

All Articles