简体   繁体   中英

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 ?

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM