SQL - INSERT and catch the id auto-increment value
It depends on your database server. Using MySQL, call mysql_insert_id()
immediately after your insert query. Using PostgreSQL, first query "select nextval(seq)
" on the sequence and include the key in your insert query.
Querying for "select max(id) + 1 from tbl
" could fail if another request inserts a record simultaneously.
In postgres the best way is to do something like:
insert into foos(name) values ('my_foo') returning id;