tags:

views:

72

answers:

1

Possible Duplicate:
mysql_insert_id alternative for postgresql

Is there a Postgresql equivalent of mysql_insert_id() to get the ID generated in the last query ??

+3  A: 

If you are using PostgreSQL 8.2 or newer then the simplest way is to use RETURNING:

$result = pg_query($db, "INSERT INTO foo (bar) VALUES (123) RETURNING foo_id");
$insert_row = pg_fetch_row($result);
$insert_id = $insert_row[0];

For other alternatives see this duplicate.

Mark Byers