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)
More precisely,
mysql_insert_id() is updated
under these 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; if no such value is
generated, it returns the last explicit
value inserted into the AUTO_INCREMENT
column.
INSERT statements that
generate an AUTO_INCREMENT value by
inserting
LAST_INSERT_ID(
into any column or by updating any column to
expr)LAST_INSERT_ID(.
expr)
If the previous statement returned an error, the value of
mysql_insert_id() is
undefined.
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 not affected by statements such as
SELECT that return a result set.
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.
The LAST_INSERT_ID() SQL function
returns the most recently generated
AUTO_INCREMENT value, and is not reset
between statements because the value of that function is
maintained in the server. Another difference from
mysql_insert_id() is that
LAST_INSERT_ID() is not updated
if you set an AUTO_INCREMENT column to a
specific nonspecial value. See
Section 11.10.3, “Information Functions”.
The reason for the differences between
LAST_INSERT_ID() and
mysql_insert_id() is that
LAST_INSERT_ID() is made easy to
use in scripts while
mysql_insert_id() tries to
provide more exact information about what happens to the
AUTO_INCREMENT column.
Return Values
Described in the preceding discussion.
Errors
None.

User Comments
Add your own comment.