PHP, Postgres help using RETURNING

风流意气都作罢 提交于 2019-12-09 10:11:53

问题


I think I understand how PostgreSQL and RETURNING works - I've found many, many resources. If I'm catching on, it would look something like

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

However, I can't find anything that helps me access this via PHP. When I thought I figured it out, 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 executing and declaring the variable separately with one query. After looking for hours for the solution, I've settled on a max(id) SELECT statement/function, but it's still bothering me. Any help is greatly appreciated.

I'm using Postgres 8.4 and PHP 5.3.


回答1:


$new_id does not contain the id but it is a resource descriptor. You need to fetch the data from it as the query would be a SELECT, with pg_fetch_array($new_id) by example.

The RETURNING clause of PostgreSQL projects any fields of the inserted or modified rows ie INSERT|UPDATE … RETURNING id, field1, field2.



来源:https://stackoverflow.com/questions/13070752/php-postgres-help-using-returning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!