MySQL Last Inserted ID
Monday, 18. September 2006, 00:16:55
The common way :
$query = "INSERT INTO table (id, value) VALUES ('', 'value')";
mysql_query($query) or die(mysql_error());
$id = mysql_result((mysql_query("SELECT * FROM table WHERE value = 'value'")), 0, 'id');
echo "The last inserted ID was :" . $id;
The easy way :
$query = "INSERT INTO table (id, value) VALUES ('', 'value')";
mysql_query($query) or die(mysql_error());
echo "The last inserted ID was :" . mysql_insert_id();
A useful snippet if you aren't familiar with the 'mysql_insert_id()' function. This will save you some coding and time if you've been using other common, longer ways in the past.















