23.8.7.37 mysql_insert_id()

my_ulonglong mysql_insert_id(MYSQL *mysql)

Description

Returns the value generated for an
AUTO_INCREMENT column by the previous
INSERT or
UPDATE statement. Use this
function after you have performed an
INSERT statement into a table
that contains an AUTO_INCREMENT field, or
have used INSERT or
UPDATE to set a column value
with
LAST_INSERT_ID(expr).

The return value of
mysql_insert_id() is always
zero unless explicitly updated under one of the following
conditions:

INSERT statements that
store a value into an AUTO_INCREMENT
column. This is true whether the value is automatically
generated by storing the special values
NULL or 0 into the
column, or is an explicit nonspecial value.

In the case of a multiple-row
INSERT statement,
mysql_insert_id() returns
the first automatically generated
AUTO_INCREMENT value that was
successfully inserted.

If there is an AUTO_INCREMENT
column in the table and there were some explicit
successfully inserted values or some updated values,
return the last of the inserted or updated values.

mysql_insert_id() returns
0 if the previous statement does not use an
AUTO_INCREMENT value. If you need to save
the value for later, be sure to call
mysql_insert_id() immediately
after the statement that generates the value.

The value of mysql_insert_id()
is affected only by statements issued within the current
client connection. It is not affected by statements issued by
other clients.

mysql_insert_id() returns
0 following a
CALL statement for a stored
procedure that generates an AUTO_INCREMENT
value because in this case
mysql_insert_id() applies to
CALL and not the statement
within the procedure. Within the procedure, you can use
LAST_INSERT_ID() at the SQL
level to obtain the AUTO_INCREMENT value.