[SOLVED] Finding record number of newly inserted 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
davidtee1
Forum Newbie
Posts: 18
Joined: Fri Jun 10, 2005 2:41 am

[SOLVED] Finding record number of newly inserted record

Post by davidtee1 »

I use an auto increment field to uniquely identify my records in a MySQL table.

When I insert a new record, a new unique id is created but how can I find out what the number is so I can refer to it immediately?

Any help gratefully received.
Last edited by davidtee1 on Sun Jun 12, 2005 8:42 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 »

refer to http://www.php.net/manual/en/function.m ... ert-id.php

to get the last inserted id
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

Code: Select all

// insert statement + call here
$lastID = mysql_insert_id();
this is incredibly useful on auto-incrementing ID rows
davidtee1
Forum Newbie
Posts: 18
Joined: Fri Jun 10, 2005 2:41 am

Post by davidtee1 »

Thanks guys. Brilliant. I knew it would be in there somewhere!

David
Post Reply