gpt4 book ai didi

php - 我可以相信 mysql_insert_id() 返回正确的值,多线程偏执狂

转载 作者:行者123 更新时间:2023-11-29 04:07:43 26 4
gpt4 key购买 nike

我创建了一个向表中插入新行的函数,在将 INSERT 语句调用到 mysql 之后,我进行了一些检查,然后我检查了 mysql_insert_id() 的值

我想安静地知道,在我得到 mysql_insert_id() 的结果之前,另一个线程不可能插入一行。

我打赌它应该是安全的,但我问是因为这可能是灾难性的。

最佳答案

来自manual :

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. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.

因此,如果您的线程将使用单独的连接,它们将获得不同的插入 ID。

关于php - 我可以相信 mysql_insert_id() 返回正确的值,多线程偏执狂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25505200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com