Postgres/PHP - What is the standard way to retrieve the ID of an inserted row?

lRamonab

New Member
I have searched on Google and read a couple of articles on how different people approach this problem, but I was wondering what the standard way of solving it is and also, which one will work best for my situation.I have an AJAX page that creates a new question and I need to know how to retrieve the ID from the insert query within the same php file, on the next line.It looks something like this:\[code\]$r = pg_query("INSERT INTO questions (audit_id, type_id, order) VALUES (1,1,1)");// Fetch ID from $r here...\[/code\]I have seen the \[code\]mysql_insert_id()\[/code\] function for MySQL and heard that \[code\]pg_last_oid()\[/code\] is similar for PostgreSQL, but the documentation claims that it is deprecated and will be removed soon. I have also seen the use of \[code\]CURRVAL('my_sequence_table.id')\[/code\] but I'm not sure if this will work with the AJAX since that might raise a race condition.Can somebody please tell me the standard PHP/PostgreSQL way to solve this problem? I would greatly appreciate any comments.P.S. I miss Ruby on Rails!
 
Back
Top