mysql_insert_id() && LAST_INSERT_ID()
Used when you add a record in to one of the table using PHP, and then you want to use that ‘auto generated record id’ for further modifications to other table.Used when you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.
When a new AUTO_INCREMENT value has been generated, you can also obtain it by executing a SELECT LAST_INSERT_ID() statement with mysql_query() and retrieving the value from the result set returned by the statement.
For LAST_INSERT_ID(), the most recently generated ID is maintained in the
server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a non-magic value (that is, a value that is not NULL and not 0).
Note that mysql_insert_id() returns the value stored into an AUTO_INCREMENT column, whether that value is automatically generated by storing NULL or 0 or was specified as an explicit value. LAST_INSERT_ID() returns only automatically generated AUTO_INCREMENT values. If you store an explicit value other than NULL or 0, it does not affect the value returned byLAST_INSERT_ID().
mysql_insert_id(PHP 3, PHP 4, PHP 5)
mysql_insert_id -- Get the ID generated from the previous INSERT operation
The ID generated for an AUTO_INCREMENT column by the previous INSERT query on success,
0 if the previous query does not generate an AUTO_INCREMENT value, or
FALSE if no MySQL connection was established.
Note: Because
mysql_insert_id() acts on the last performed query, be sure to call
mysql_insert_id() immediately after the query that generates the value.
Note: The value of the MySQL SQL function
LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries.