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:
$r = pg_query("INSERT INTO questions (audit_id, type_id, order) VALUES (1,1,1)");
// Fetch ID from $r here...
I have seen the mysql_insert_id()
function for MySQL and heard that pg_last_oid()
is similar for PostgreSQL, but the documentation claims that it is deprecated and will be removed soon. I have also seen the use of CURRVAL('my_sequence_table.id')
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!