That actually isn't a safe way to do it. Consider the following scenario:
Insert A occurs generating row Y in your table.
Insert B occurs generating row Z in your table.
select occurs (after insert A) to grab last ID inserted...your result is the
ID from row Z.
select occurs (after insert B), and your result again is the result ID from
So, in both cases you will get the ID from insert B, instead of your
intended result (first select gives the ID from row Y and the second from
You didn't mention which database engine that you are using, so I will
assume you are using MySQL. There is a function called mysql_insert_id()
that will give you the answer that you are looking for. You can find more
information about it here:
Software Quality Assurance Engineer
(716) 853-1362 X289
----- Original Message -----
From: "George Pitcher" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, January 14, 2002 9:23 AM
Subject: [PHP-DB] grabbing id from new record
> Is there a way to grab the ID (from ID field) while INSERTing a new
> I am currently doing a separate INSERT then a SELECT query to find the
> recently added record by that user:
> select ID from table where user = '$user' order by ID DESC limit 0,1
> This does the trick but it may not be the most efficient way.
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]