Get Last Inserted Id

There are many cases where we need AUTO_INCREMENT ID that generated from the previous INSERT operation, we can get that by using mysql_insert_id() function.

$con = mysql_connect(“localhost”, “root”, “”);
$query = ”insert into emp (name,salary,phone) values (‘amit’,9000,’9847476744’)”;
echo “Last inserted id is ”. mysql_insert_id();

Some time programmer use “select max(id) from tablename” to get the last inserted id, but it may return wrong result because max(id) may not be last inserted id while transaction going on OR records inserting on.

Leave a Reply