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

标签 php adodb lastinsertid insert-id

我正在为 PHP 库使用 adodb。

为了获取插入记录的 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 - 我如何处理 mysql 表中的并发插入并获取正确的插入 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2124200/

相关文章:

php - 找不到 Laravel 5.1 路由对象

php - where 子句中的 Codeigniter 列 'idimport_database' 不明确

excel - 来自 VBA 的 ADODB 连接停止工作

php - mysql_last_id() 的竞争条件

php - PDO lastInsertId() 总是返回 0

orm - Kohana 3 ORM检索最后一个插入ID

php - 制作对象并将其传递到 codeigniter View

php - 在 PHP 中,为什么 json_encode 比序列化慢?

sql - 使用SQL查询Excel电子表格时如何指定多个标题行?

excel - 如何在 ADODB SQL 查询中连接到 Excel 命名范围