PHP,Postgres帮助使用RETURNING [英] PHP, Postgres help using RETURNING
问题描述
我想我了解PostgreSQL和RETURNING的工作原理-我发现了很多资源。如果我能继续前进,它看起来就像
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;"
但是,我找不到任何可以帮助我通过PHP进行访问的内容。当我以为自己想通了,我尝试了
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;
但它返回NULL。我还尝试通过一个查询分别执行和声明变量。在寻找了几个小时的解决方案之后,我决定使用max(id)SELECT语句/函数,但它仍然困扰着我。
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.
我正在使用Postgres 8.4和PHP 5.3。
I'm using Postgres 8.4 and PHP 5.3.
推荐答案
$ new_id
不包含ID,但它是资源描述符。您需要从中获取数据,因为查询将是SELECT,例如,使用 pg_fetch_array($ new_id)
。
$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.
PostgreSQL的 RETURNING
子句会投影插入或修改的行的任何字段,即 INSERT | UPDATE…RETURNING id,field1,field2
。
The RETURNING
clause of PostgreSQL projects any fields of the inserted or modified rows ie INSERT|UPDATE … RETURNING id, field1, field2
.
这篇关于PHP,Postgres帮助使用RETURNING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!