I am using MySQL.I have to get the last inserted record's primary key value. How can i get this?
This will return the last inserted id.
SELECT LAST_INSERT_ID();
OR
if ((result = mysql_store_result(&mysql)) == 0 &&
mysql_field_count(&mysql) == 0 &&
mysql_insert_id(&mysql) != 0)
{
used_id = mysql_insert_id(&mysql);
}
mysqli_insert_id()
PDO::lastInsertId()
mysql_insert_id()
It's well worth checking the documentation if you are likely to meet "race" conditions. http://uk3.php.net/manual/en/function.mysql-insert-id.php etc.
Use follwoing query:
SELECT LAST_INSERT_ID();
There are two function that return this value:
mysql_insert_id()
--> PHP function
LAST_INSERT_ID()
--> MySQL function that can be used by PHP and other Languages or in MySQL command line interface or MySQL front-end