PDA

View Full Version : How to get values of last MySQL insert query


andre
02-14-2000, 10:44 AM
I have a slight problem with mysql. How do I get the values of the row I just inserted?
I have a table with two columns id (auto_increment) and names.
After I insert some values from a php page, eg:

$query="INSERT INTO table (names) VALUES ('johnboy')";
$res=mysql_query($query) or die("oops?");


How do I get the value of 'id' of that query? I am a little bit slow today and this is driving me mad at the moment. :(

Justin
02-14-2000, 11:07 AM
$ID = mysql_insert_id ($result);

Hope this helps.

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

andre
02-14-2000, 11:14 AM
That was it. Thank you!

BTW: I like the Internet. You can't see people blush in front of their screens...;)[nbsp]

Justin
02-14-2000, 05:15 PM
Yes, I like that particular "feature" myself :)[nbsp][nbsp]I remember not knowing that little detail myself, and I actually had one of my scripts doing a search and finding the highest ID number, and assuming that was the one. This of course worked as long as two people didn't catch the script at just the right (wrong) time... then of course I felt really silly when I found out PHP does that automatically :)

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