PDA

View Full Version : can sql insert return auto_increment id


jbroder
02-18-2000, 02:03 PM
I have a script that adds a row to a table. The table's primary key is an auto_increment field called ID.

As soon as I successfully insert the row, I want to know what the ID is.

The way I've implemented this is to do a separate query based on a field that I just inserted that is unique

(select ID from TABLE where UNIQUE_FIELD = $FieldIJustEntered)

Which means that one field be required unique and that I have to do separate error handling for the 2nd query. Besides this seems like the hard way to do it.

Is there an easier better way to do this?
like

(INSERT INTO TABLE blah, blah, woof, woof, RETURN ID)

thanks for any help...

Jon
------------------
guitartricks.com

Justin
02-18-2000, 02:35 PM
If you are using PHP:
</font><font face="Courier" size="3">
$result = mysql_query (&quot;insert into foo values (NULL, 'foo', 'bar')&quot;);
$ID = mysql_insert_id ($result);
</font><font face="Verdana, Arial" size="2">
Hope this helps :)

------------------
Justin Nelson
FutureQuest (http://www.FutureQuest.net/index.php) Support