If you insert a record that has a AUTO_INCREMENT index then you
can get the given id with mysql_insert_id(MYSQL *).
The last value is also stored in the server and can be retrieved with
the LAST_INSERT_ID() function.
You can check if an auto_increment index is used by the following
code. This also checks if the query was an INSERT with an
auto_increment index.
if (mysql_error(MYSQL)[0] == 0 &&
mysql_num_fields(MYSQL_RESULT) == 0 &&
mysql_insert_id(MYSQL) != 0)
used_id = mysql_insert_id(MYSQL);
The id that LAST_INSERT_ID() returns is maintained in the server
per connection. It will not be botched by another client. It will not
even be changed if you update another auto_increment column with a non
magic value (that is a not NULL or 0).
Go to the first, previous, next, last section, table of contents.