mysql_insert_id();
An example using a temporary table is shown here:
<?php $conn = mysql_connect('localhost', 'username', 'password') or die ('Error connecting to mysql'); mysql_select_db('myDatabase'); mysql_query("CREATE TEMPORARY TABLE testTable (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), column1 VARCHAR(50) )") or die (mysql_error()); mysql_query("INSERT INTO testTable VALUES ()"); $id = mysql_insert_id(); echo $id; //Outputs 1 mysql_query("INSERT INTO testTable VALUES ()"); $id = mysql_insert_id(); echo $id; //Outputs 2 ?>
Simple and useful PHP ;)
I use a temporary table in the example to demonstrate the concept, so that in case you do decide to test out the example, it will not leave a footprint on your MySQL server, since temporary tables are deleted when the MySQL session is over.