How to retrieve id number when inserting new record

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
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

How to retrieve id number when inserting new record

Post by 9902468 »

I'msaving a new order and I need the id number of the order, so that I can later add more things to this order.

Is this bulletproof?

Code: Select all

start transaction;
execute insert;
if (insert succesfull){
 execute select current(sequence_name) as number;
  if (select succesfull){
        commit;
  }else{
        rollback;
        (error handling)
  } 

}else{
  rollback;
  (error handling)
}
i'm worried that if there are two or more new order creations at the same time, will I get the right number for every order?

-9902468
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

According to MySQL

Post by EricS »

According to MySQL, LAST_INSERT_ID is supposed to return the last auto_increment value for the table specified on a session basis. I'm guessing that if two different visitors create an entry at the same time, then each will get back their own ID.

If anyone one else knows for sure, let us know, I'm currently in the same problem you are.
Post Reply