mysql_insert_id() - 0
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).AUTO_INCREMENTINSERTINSERTUPDATEUPDATEINSERTINSERTAUTO_INCREMENTINSERTINSERTUPDATEUPDATELAST_INSERT_ID(expr)LAST_INSERT_ID(expr)exprexprThe return value of
mysql_insert_id() is always zero
unless explicitly updated under one of the following conditions:mysql_insert_id()mysql_insert_id()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 no rows are successfully inserted,
mysql_insert_id() returns 0.
If an INSERT
... SELECT statement is executed, and no
automatically generated value is successfully inserted,
mysql_insert_id() returns
the ID of the last inserted row.
If an INSERT
... SELECT statement uses
LAST_INSERT_ID(expr),
mysql_insert_id() returns
expr.
INSERT statements that
generate an AUTO_INCREMENT value by
inserting
LAST_INSERT_ID(expr)
into any column or by updating any column to
LAST_INSERT_ID(expr).
If the previous statement returned an error, the value of
mysql_insert_id() is
undefined.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 no rows are successfully inserted,
mysql_insert_id() returns 0.
If an INSERT
... SELECT statement is executed, and no
automatically generated value is successfully inserted,
mysql_insert_id() returns
the ID of the last inserted row.
If an INSERT
... SELECT statement uses
LAST_INSERT_ID(expr),
mysql_insert_id() returns
expr.
INSERT statements that
generate an AUTO_INCREMENT value by
inserting
LAST_INSERT_ID(expr)
into any column or by updating any column to
LAST_INSERT_ID(expr).
If the previous statement returned an error, the value of
mysql_insert_id() is
undefined.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.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.INSERTINSERTAUTO_INCREMENTNULL0In the case of a multiple-row
INSERT statement,
mysql_insert_id() returns
the first automatically generated
AUTO_INCREMENT value that was
successfully inserted.
If no rows are successfully inserted,
mysql_insert_id() returns 0.In the case of a multiple-row
INSERT statement,
mysql_insert_id() returns
the first automatically generated
AUTO_INCREMENT value that was
successfully inserted.INSERTINSERTmysql_insert_id()mysql_insert_id()AUTO_INCREMENTIf no rows are successfully inserted,
mysql_insert_id() returns 0.mysql_insert_id()mysql_insert_id()If an INSERT
... SELECT statement is executed, and no
automatically generated value is successfully inserted,
mysql_insert_id() returns
the ID of the last inserted row.If an INSERT
... SELECT statement is executed, and no
automatically generated value is successfully inserted,
mysql_insert_id() returns
the ID of the last inserted row.INSERT
... SELECTINSERT
... SELECTmysql_insert_id()mysql_insert_id()If an INSERT
... SELECT statement uses
LAST_INSERT_ID(expr),
mysql_insert_id() returns
expr.If an INSERT
... SELECT statement uses
LAST_INSERT_ID(expr),
mysql_insert_id() returns
expr.INSERT
... SELECTINSERT
... SELECTLAST_INSERT_ID(expr)LAST_INSERT_ID(expr)exprexprmysql_insert_id()mysql_insert_id()exprexprINSERT statements that
generate an AUTO_INCREMENT value by
inserting
LAST_INSERT_ID(expr)
into any column or by updating any column to
LAST_INSERT_ID(expr).INSERT statements that
generate an AUTO_INCREMENT value by
inserting
LAST_INSERT_ID(expr)
into any column or by updating any column to
LAST_INSERT_ID(expr).INSERTINSERTAUTO_INCREMENTLAST_INSERT_ID(expr)LAST_INSERT_ID(expr)exprexprLAST_INSERT_ID(expr)LAST_INSERT_ID(expr)exprexprIf the previous statement returned an error, the value of
mysql_insert_id() is
undefined.If the previous statement returned an error, the value of
mysql_insert_id() is
undefined.mysql_insert_id()mysql_insert_id()The return value of
mysql_insert_id() can be
simplified to the following sequence:mysql_insert_id()mysql_insert_id()If there is an AUTO_INCREMENT column, and
an automatically generated value was successfully inserted,
return the first such value.
If
LAST_INSERT_ID(expr)
occurred in the statement, return
expr, even if there was an
AUTO_INCREMENT column in the affected
table.
The return value varies depending on the statement used.
When called after an INSERT
statement:
If there is an AUTO_INCREMENT column
in the table, and there were some explicit values for
this column that were successfully inserted into the
table, return the last of the explicit values.
When called after an
INSERT
... ON DUPLICATE KEY UPDATE statement:
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.If there is an AUTO_INCREMENT column, and
an automatically generated value was successfully inserted,
return the first such value.
If
LAST_INSERT_ID(expr)
occurred in the statement, return
expr, even if there was an
AUTO_INCREMENT column in the affected
table.
The return value varies depending on the statement used.
When called after an INSERT
statement:
If there is an AUTO_INCREMENT column
in the table, and there were some explicit values for
this column that were successfully inserted into the
table, return the last of the explicit values.
When called after an
INSERT
... ON DUPLICATE KEY UPDATE statement:
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.If there is an AUTO_INCREMENT column, and
an automatically generated value was successfully inserted,
return the first such value.If there is an AUTO_INCREMENT column, and
an automatically generated value was successfully inserted,
return the first such value.AUTO_INCREMENTIf
LAST_INSERT_ID(expr)
occurred in the statement, return
expr, even if there was an
AUTO_INCREMENT column in the affected
table.If
LAST_INSERT_ID(expr)
occurred in the statement, return
expr, even if there was an
AUTO_INCREMENT column in the affected
table.LAST_INSERT_ID(expr)LAST_INSERT_ID(expr)exprexprexprexprAUTO_INCREMENTThe return value varies depending on the statement used.
When called after an INSERT
statement:
If there is an AUTO_INCREMENT column
in the table, and there were some explicit values for
this column that were successfully inserted into the
table, return the last of the explicit values.
When called after an
INSERT
... ON DUPLICATE KEY UPDATE statement:
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.The return value varies depending on the statement used.
When called after an INSERT
statement:INSERTINSERTIf there is an AUTO_INCREMENT column
in the table, and there were some explicit values for
this column that were successfully inserted into the
table, return the last of the explicit values.If there is an AUTO_INCREMENT column
in the table, and there were some explicit values for
this column that were successfully inserted into the
table, return the last of the explicit values.If there is an AUTO_INCREMENT column
in the table, and there were some explicit values for
this column that were successfully inserted into the
table, return the last of the explicit values.If there is an AUTO_INCREMENT column
in the table, and there were some explicit values for
this column that were successfully inserted into the
table, return the last of the explicit values.AUTO_INCREMENTWhen called after an
INSERT
... ON DUPLICATE KEY UPDATE statement:INSERT
... ON DUPLICATE KEY UPDATEINSERT
... ON DUPLICATE KEY UPDATEIf 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.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.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.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.AUTO_INCREMENTmysql_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.mysql_insert_id()mysql_insert_id()0AUTO_INCREMENTmysql_insert_id()mysql_insert_id()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()mysql_insert_id()The LAST_INSERT_ID() SQL function
will contain the value of the first automatically generated
value that was successfully inserted.
LAST_INSERT_ID() 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 13.14, “Information Functions”.LAST_INSERT_ID()LAST_INSERT_ID()LAST_INSERT_ID()LAST_INSERT_ID()mysql_insert_id()mysql_insert_id()LAST_INSERT_ID()LAST_INSERT_ID()AUTO_INCREMENTSection 13.14, “Information Functions”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.mysql_insert_id()mysql_insert_id()0CALLCALLAUTO_INCREMENTmysql_insert_id()mysql_insert_id()CALLCALLLAST_INSERT_ID()LAST_INSERT_ID()AUTO_INCREMENTThe 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.LAST_INSERT_ID()LAST_INSERT_ID()mysql_insert_id()mysql_insert_id()LAST_INSERT_ID()LAST_INSERT_ID()mysql_insert_id()mysql_insert_id()AUTO_INCREMENT
Syntax
my_ulonglong mysql_insert_id(MYSQL *mysql)
Example
Output / Return Value
Described in the preceding discussion.
Limitations
Alternatives / See Also
Reference