user1119096
user1119096

Reputation: 103

What exactly does mysql_insert_id() return?

What would mysql_insert_id() return in case some other query was made in the meantime? For example:

mysql_query('SOME INSERT1');

In the meantime, the other instance of this script calls mysql_query('SOME INSERT2');, and in 1st instance:

echo mysql_insert_id();

Will this return the id of the first or the second insert?

Upvotes: 1

Views: 1444

Answers (5)

Vyktor
Vyktor

Reputation: 20997

Each instance uses it's own connection and own connection identification, so the threads shouldn't be interfering and function should return insert id from last query.

Upvotes: 1

rkosegi
rkosegi

Reputation: 14638

It will return insert ID for your database connection session.

Please read this :

http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

and this:

Concurrency Problem With MySql Last_Insert_Id()... (C#)

Upvotes: 0

confucius
confucius

Reputation: 13327

It will return the last id that auto incremented by mysql

Upvotes: 0

RMcLeod
RMcLeod

Reputation: 2581

mysql_insert_id will return the id of the last inserted row. If you need to get the last insert id then make sure you do it before any other inserts are done on that table

Upvotes: 0

Matt Healy
Matt Healy

Reputation: 18531

The manual says:

Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).

Note:

Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.

Upvotes: 0

Related Questions