MySQL - Find the Insert ID
$newid = mysql_insert_id($dbh);
<!-- EXAMPLE //-->
mysql_select_db($dbname);
$sql = "INSERT INTO users(name, username, password) VALUES('$name', '$un', '$pw')";
$result = mysql_query($sql, $dbh);
$uid = mysql_insert_id($dbh);
mysql_close($dbh);
COMMENTS Very useful if you need to update other tables when you've just added something like a new user to your database without having to run another Select query.
|