Wow, thanks for such a great resposne! Sorry I should have clarified that I am using MySQL. The mysql_insert_id() suggestion worked perfectly and reduces the amount of code I'm running from about 12 down to 1! Thanks for all the help, it's greatly appreciated. TimC > If you are running a high traffic application this will cause you a world > of > problems. Do not do it unless you like pulling out your hair and banging > your head against the wall. What happens if 3 records are inserted at the > same time or just microseconds apart? You are going to get the wrong > insert > ID for 2 of them. Been there, done that, spent 15 hours straight at work > fixing the problem. > >> -----Original Message----- >> From: Luke Woollard [mailto:luke@taborvision.com] >> Sent: Thursday, April 10, 2003 5:09 PM >> To: PHP-db >> Cc: timbo@komrades.dyndns.org >> Subject: RE: Getting the value of an auto-increment >> Importance: High >> >> >> Try this immediately after you insert a row. >> >> $sqlQuery = "select MAX(YOUR_COLUMN_NAME) from YOUR_TABLE_NAME"; >> >> >> >> >> -----Original Message----- >> From: timbo@komrades.dyndns.org [mailto:timbo@komrades.dyndns.org] >> Sent: Friday, 11 April 2003 9:59 AM >> To: >> Subject: Getting the value of an auto-increment >> >> >> Hello, >> >> I'm using an auto-increment field in a table as the primary key. What I >> want to know is when I use an INSERT query to add a row to the table is >> there a way I can get the value of the auto-increment field of the row >> that was just inserted? >> >> What I'm currently doing is messy but works: >> I get the current time and session id and write them to one of the >> fields >> in the row and INSERT that. I then do a query and find the row (thus >> getting the value of the auto-increment field) and then write the real >> data. >> >> I'm just hoping there's an easier way. >> >> Thanks in advance, >> >> Tim Coombes >> Komrades Ink. >> >> -- >> PHP Database Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> -- >> PHP Database Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php