postgresql PHP、Postgres 帮助使用 RETURNING
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13070752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP, Postgres help using RETURNING
提问by regan_leah
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
我想我了解 PostgreSQL 和 RETURNING 的工作原理 - 我找到了很多很多资源。如果我赶上,它看起来像
"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
但是,我找不到任何可以帮助我通过 PHP 访问它的东西。当我以为我想通了时,我尝试了
$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.
但它返回NULL。我还尝试使用一个查询单独执行和声明变量。在寻找解决方案数小时后,我确定了一个 max(id) SELECT 语句/函数,但它仍然困扰着我。任何帮助是极大的赞赏。
I'm using Postgres 8.4 and PHP 5.3.
我使用的是 Postgres 8.4 和 PHP 5.3。
回答by greg
$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.
$new_id
不包含 id 但它是一个资源描述符。您需要从中获取数据,因为查询将是一个 SELECT,pg_fetch_array($new_id)
例如。
The RETURNING
clause of PostgreSQL projects any fields of the inserted or modified rows ie INSERT|UPDATE … RETURNING id, field1, field2
.
RETURNING
PostgreSQL的子句投影插入或修改的行的任何字段,即INSERT|UPDATE … RETURNING id, field1, field2
.