I need to select the last row inserted into a database immediately after writing it. I have a form that, when submitted, generates an auto-incremented key. I then need to select this key so that I can email it to the submitter. I tried querying the db AFTER executing the insert, but i don’t get any results. I’ve double checked my queries and they are sound by themselves. Am I missing something? My code looks like this:
$query = "INSERT INTO users VALUES (NULL, '".$name."', '".$email."')";
$result = $db->query($query);
$query = "SELECT MAX(userid) as userid FROM users";
$result = $db->query($query);
My problem is that the second $result doesn’t return any results.
You can get the value like this:
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id