数据库
首页 > 数据库> > php – 我如何处理mysql表中的并发插入并获取正确的插入ID

php – 我如何处理mysql表中的并发插入并获取正确的插入ID

作者:互联网

我正在使用adodb for PHP库.

为了获取插入记录的id,我使用此函数

"$db->Insert_ID()"

我想知道是否有多个同时插入数据库表,这个方法会为每个插入的记录返回正确的插入ID吗?

我问这个的原因是因为我使用这个最后一个插入id来进一步处理其他记录并在相关表中进行后续输入.

这种方法是否足够安全,或者我错过了什么.

请帮我为此制定一个正确的工作计划,以便我可以使用最后一个插入ID进一步插入到另一个表中,而不必弄乱现有数据.

谢谢

解决方法:

是的,对于使用它是安全的.这是因为LAST_INSERT_ID()是每个连接,如here所述:

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.

标签:php,lastinsertid,adodb,insert-id
来源: https://codeday.me/bug/20190715/1472859.html