| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
mysql_insert_id()
my_ulonglong mysql_insert_id(MYSQL *mysql)
Returns the value generated for an AUTO_INCREMENT column by the
previous INSERT or UPDATE query. Use this function after
you have performed an INSERT query into a table that contains an
AUTO_INCREMENT field.
Note that mysql_insert_id() returns 0 if the previous query
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 query that generates the value.
If the previous query returned an error, the value of mysql_insert_id()
is undefined.
mysql_insert_id() is updated after:
INSERT statements that sets or generates an AUTO_INCREMENT value.
In case of multi-row INSERT statement mysql_insert_id() returns
the FIRST generated AUTO_INCREMENT value or the LAST
set AUTO_INCREMENT value if there was no generated value.
UPDATE statements that generate an AUTO_INCREMENT value.
INSERT or UPDATE statements that set a column value to
LAST_INSERT_ID(expr).
See section 12.6.4 Miscellaneous Functions.
Also note that the value of the SQL LAST_INSERT_ID() function always
contains the most recently generated AUTO_INCREMENT value, and is
not reset between queries because the value of that function is maintained
in the server. Another difference is that LAST_INSERT_ID() is not
updated if you set an AUTO_INCREMENT column to a specific value.
The reason for the difference 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 a little
more exact information of what happens to the auto_increment column.
The value of the AUTO_INCREMENT field that was updated by the previous
query. Returns zero if there was no previous query on the connection or if
the query did not update an AUTO_INCREMENT value.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |