gpt4 book ai didi

MySQL 和 LAST_INSERT_ID() 返回错误 id 的可能性

转载 作者:IT老高 更新时间:2023-10-29 00:12:12 24 4
gpt4 key购买 nike

根据我的阅读 LAST_INSERT_ID() 检索要运行的最后一个插入语句的 id。如果最后一个插入语句在您的连接上运行,或者最后一个插入在数据库上运行所有连接?我想我想问的是:在一个繁忙的数据库驱动网站上,以下代码返回错误 id 的可能性有多大,是否有其他方法可以防止锁定数据库?

INSERT INTO example (string) VALUE ('something');
SELECT LAST_INSERT_ID();

PHP 的 mysql_insert_id() 会是更好的解决方案,还是我应该只查询刚刚插入的数据并以这种方式获取 id?

最佳答案

Is that the last insert statement run on your connection or...

连接上的 last_insert。

Would PHP's mysql_insert_id() be a better solution

不,是一样的。

or should I just query for data that was just inserted and grab id that way?

这样比较慢

链接:http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

从该链接引用:

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.

关于MySQL 和 LAST_INSERT_ID() 返回错误 id 的可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5917860/

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