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 row Z. 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 row Z). 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: http://www.php.net/manual/en/function.mysql-insert-id.php HTH Sam Masiello Software Quality Assurance Engineer Synacor (716) 853-1362 X289 [EMAIL PROTECTED] ----- 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 > Hi, > > Is there a way to grab the ID (from ID field) while INSERTing a new record? > > I am currently doing a separate INSERT then a SELECT query to find the most > 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. > > George > > > --- > 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]