"Michael" <com> wrote:
Nobody is doing *that* anyway. There is no reliable way to answer
"What AUTO_INCREMENT value will I get if I execute $INSERT now?".
Just think of race conditions with other clients inserting data.
The ususal way is to just do the INSERT and then ask MySQL
"What AUTO_INCREMENT did you create for my last INSERT?"
MySQL answers that with the LAST_INSERT_ID() function:
PHP gives you another possibility: use the mysql_insert_id() function:
The difference between the LAST_INSERT_ID() SQL function and the
mysql_insert_id() API function is, that LAST_INSERT_ID() remembers
the latest generated AUTO_INCREMENT value, even if there were other
statements in between. OTOH mysql_insert_id() remembers the
AUTO_INCREMENT value of the last statement. If you do some SQL
statement that does not generate an AUTO_INCREMENT value, it will
reset mysql_insert_id() to 0.
Beware of the implicite connection sharing in PHP! If you use
multiple database connections in the same PHP script, PHP may share
connections (see here for an explanation)
This may spoil AUTO_INCREMENT values, even those from LAST_INSERT_ID.
Axel Schwenke, Support Engineer, MySQL AB
Online User Manual: http://dev.mysql.com/doc/refman/5.0/en/
MySQL User Forums: http://forums.mysql.com/