is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks are solving the problem partially by finding the current value of the sequence used in the ID. However, there are times that the primary key is not a serial column….
is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks
Share
From the PostgreSQL point of view, in pseudo-code:
pg_last_oid()only works where you have OIDs. OIDs have been off by default since PostgreSQL 8.1.So, depending on which PostgreSQL version you have, you should pick one of the above method. Ideally, of course, use a database abstraction library which abstracts away the above. Otherwise, in low level code, it looks like:
Method one: INSERT… RETURNING
Method two: INSERT; lastval()
Method three: nextval(); INSERT
The safest bet would be the third method, but it’s unwieldy. The cleanest is the first, but you’d need to run a recent PostgreSQL. Most db abstraction libraries don’t yet use the first method though.