mysql_insert_id
Get the ID generated by the previous INSERT operation.
Function name: mysql_insert_id()
Function Description: This function is used to obtain the autoincrement ID value of the last inserted record.
Applicable version: This function was introduced in PHP 4, but was deprecated in PHP 5.5.0 and removed in PHP 7.0.0. It is recommended to use mysqli or PDO extension instead.
Syntax: mysql_insert_id($link)
parameter:
Return value: Returns an integer representing the self-increment ID value of the last inserted record.
Example:
$link = mysql_connect("localhost", "username", "password"); mysql_select_db("database", $link); $query = "INSERT INTO users (name) VALUES ('John')"; mysql_query($query, $link); $lastInsertedId = mysql_insert_id(); echo "Last inserted ID: " . $lastInsertedId;
$link = mysql_connect("localhost", "username", "password"); mysql_select_db("database", $link); $query = "INSERT INTO users (name) VALUES ('John')"; mysql_query($query, $link); $lastInsertedId = mysql_insert_id($link); echo "Last inserted ID: " . $lastInsertedId;
Notes: