This function returns the AUTO_INCREMENT ID generated from the previous INSERT operation.

Syntax: mysql_insert_id(connection)

Description:

The argument “connection” is Optional. It specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

This function returns 0 if the previous operation does not generate an AUTO_INCREMENT ID, or FALSE on
MySQL connection failure.

Example:
Code:

$con = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$con)
   {
  die('Could  not connect:' . mysql_error());
   }

$db_selected = mysql_select_db("company",$con);

$sql = "INSERT INTO companydata VALUES (5,'smith',3000)";
$result = mysql_query($sql,$con);
echo "ID of last inserted record is: " . mysql_insert_id();

mysql_close($con);

Output:

ID of last inserted record is: 5

eXTReMe Tracker