Get ID of the record just added [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Get ID of the record just added [SOLVED]

Post by hairyjim »

Hi all,

I'm doing a simple record insert into my MySQL table and immediatly after the record is added I want to use the auto-incremented ID column of the table

How do I get the ID of the record I just created?

I thought about selecting the very last record in the table but this could be dangerous if I have several users all hitting the submit button at the same time.

Could a little advice be offered?
(Yes I know don't eat yellow snow...yadda yadda yadda...you funny blighters)
Last edited by hairyjim on Wed Nov 17, 2004 9:16 am, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

[php_man]mysql_insert_id[/php_man]
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Thats the one. mysql_insert_id is the way to go.

Cheers
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

powerful this php manual is. agree you do, hairyjim?
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Now you see I do agree that RTFM is the way to go but when you have no idea what subject it is under in TFM it makes it a little difficult to find the needle.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Ah. :)
Post Reply