MySQL 和 LAST_INSERT_ID() 返回错误 id 的可能性
根据我的阅读,LAST_INSERT_ID()
检索要运行的最后一个插入语句的 id。如果最后一个插入语句在您的连接上运行,或者最后一个插入语句在所有连接的数据库上运行?我想我想问的是:在一个繁忙的数据库驱动的网站上,以下代码返回错误ID的可能性有多大,除了锁定数据库之外,是否有其他方法可以防止这种情况发生?
INSERT INTO example (string) VALUE ('something');
SELECT LAST_INSERT_ID();
PHP 的 mysql_insert_id() 是一个更好的解决方案,还是我应该查询刚刚插入的数据并以这种方式获取 id?
From what I have read LAST_INSERT_ID()
retrieves the id of the last insert statement to run. If that the last insert statement run on your connection or the last insert run on the database for all connections? I guess what I am trying to ask is: on a busy database driven website what are the chances the following code would return the wrong id, and is there a method to prevent that other then locking the database?
INSERT INTO example (string) VALUE ('something');
SELECT LAST_INSERT_ID();
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的连接上的last_insert。
?不,它是一样的。
这样慢得多
链接: http:// /dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
引用该链接:
The last_insert on your connection.
No, it's the same.
That's way way slower
Link: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
Quote from that link: