我有一个插入查询。我想在插入后获取它的 id,为此我在 php 中使用 mysql_insert_id() 。
现在我正在考虑的问题是,如果两个人在两个不同的线程中使用插入查询。所以会有 4 个原子查询。
$query="INSERT INTO `info`() VALUES ()";
mysql_query($query);
echo mysql_insert_id();
如果两个插入查询先执行,然后执行mysql_insert_id()。所以我会得到最近文章的 id 和 0..
那么会出现数据不一致的情况吗?
最佳答案
LAST_INSERT_ID()使用的函数特定于连接,因此所提供的示例没有问题。
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.
但是,这个“一般问题”可以通过使用 Transactions 解决。 :咳嗽:
关于php - 同时查询的数据不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24078679/