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.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...