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 ??

Community
  • 1
  • 1
xRobot
  • 25,579
  • 69
  • 184
  • 304

1 Answers1

4

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.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452