The mysql_insert_id() function is used to get generated ID from the previous insert operation.
PHP mysql_insert_id() Function Example:
The mysql_insert_id() function is used to get generated ID from the previous insert operation.
Syntax:
mysql_insert_id(connection)
Where connection is required specified mysql connection.
Example:
<?php $connection = mysql_connect("localhost", "root", "root"); if (!$connection) { die('PHP Mysql database connection could not connect : ' . mysql_error()); } $db_selected = mysql_select_db("php_mysql", $connection); $sql_insert = "INSERT INTO user (user_name, user_email, user_address) VALUES ('brijesh', 'brijesh@gmail.com', 'Lucknow')"; $result = mysql_query($sql_insert, $connection); echo "Inserted record id is : " . mysql_insert_id(); mysql_close($connection); ?>
Before running this example the database 'user' table records is:
After running this example the browser output is:
Output:
Inserted record id is : 3
And the database 'user' table is:
[ 0 ] Comments