数据库
首页 > 数据库> > MySQL和LAST_INSERT_ID()返回错误id的可能性

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

作者:互联网

从我读过的内容中,LAST_INSERT_ID()检索要运行的最后一个insert语句的id.如果最后一个insert语句在您的连接上运行,或者最后一个insert在数据库上运行所有连接?我想我要问的是:在繁忙的数据库驱动的网站上,以下代码返回错误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.

标签:lastinsertid,mysql
来源: https://codeday.me/bug/20191006/1861168.html