Reputation:
I want to get id of current inserted row after executing insertion query.
p.s.: I'm using postgresql database.
Upvotes: 1
Views: 1027
Reputation: 3489
From the Postgres documentation
Insert a single row into table distributors, returning the sequence number generated by the DEFAULT clause:
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets') RETURNING did;
http://www.postgresql.org/docs/8.3/interactive/sql-insert.html
Upvotes: 0
Reputation: 62583
If we assume that by "id" you mean a column which is primary key and also is declared SERIAL then you need to add a RETURNING to the INSERT statement:
INSERT INTO <table> (...) VALUES (...) RETURNING id;
And in PHP you can treat this statement as a normal query which returns one row.
Upvotes: 3
Reputation: 70414
In PG7 it was pg_last_oid, the docs there explain pretty clearly how to get the last id from newer version of PG.
Upvotes: 0