Retriving the ID after a new insertion

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AbuAnas
Forum Newbie
Posts: 4
Joined: Tue Aug 19, 2003 4:48 am

Retriving the ID after a new insertion

Post by AbuAnas »

Hi

I would like to retrive the ID (auto-incrementfield) of the record that I have just insreted into the database..


I am writing a DB Class function which will try to insert a new record, if it successeded will return a record object that has the id varibale set to the new ID. If it failed to insert the new record it'll return false.
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

This depends on your DB backend...

- mysql: mysql_insert_id()
- postgresql: pg_last_oid()
- On msSQL (sybase) you'll have to query like:

Code: Select all

$id = mssql_result(mssql_query("SELECT @@IDENTITY AS id"), 0, "id");
Post Reply