goyalM
goyalM

Reputation: 83

data inconsistency for simultaneous queries

I have a insert query . And I want to fetch its id after its insertion , for which I am using mysql_insert_id() in php.

Now the problem which i was thinking about is that if two person are using the insert query in two different threads. so there will be 4 atomic query's.

$query="INSERT INTO `info`()  VALUES ()";
mysql_query($query);
echo mysql_insert_id();

If the two insert query gets executed first and then mysql_insert_id() is executed. so I will get the recent article's id and 0..

So will there be data inconsistency ?

Upvotes: 0

Views: 97

Answers (1)

user2864740
user2864740

Reputation: 61935

The LAST_INSERT_ID() function used is connection-specific so there is no problem with the presented example.

With no argument, LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column ..

The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client.

However, this "general problem" is solved by using Transactions :cough:

Upvotes: 2

Related Questions