[英]Is mysql_insert_id safe to use?
According to the PHP documentation, mysql_insert_id
takes the last inserted id from the mysql table.根据 PHP 文档,
mysql_insert_id
从 mysql 表中获取最后插入的 id。
My question is, if I have a website that inserts more than 2 rows per second to the DB, can I use the mysql_insert_id
and get the correct ID I am referring to in the INSERT
query a line before?我的问题是,如果我有一个网站每秒向数据库插入超过 2 行,我可以使用
mysql_insert_id
并获得我在一行之前在INSERT
查询中引用的正确 ID 吗?
From the MySQL manual:从 MySQL 手册:
The ID that was generated is maintained in the server on a per-connection basis.
生成的 ID 基于每个连接在服务器中维护。 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.
这意味着函数返回给给定客户端的值是为该客户端影响 AUTO_INCREMENT 列的最新语句生成的第一个 AUTO_INCREMENT 值。 This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own.
此值不会受到其他客户端的影响,即使它们生成自己的 AUTO_INCREMENT 值。 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.
此行为可确保每个客户端都可以检索自己的 ID,而无需关心其他客户端的活动,也无需锁定或事务。
Short version: it is safe to use.简短版本:使用安全。
mysql_insert_id gives you the insert id of the most recent INSERT statement on the connection you give it. mysql_insert_id 为您提供在您提供的连接上的最新 INSERT 语句的插入 ID。
If you call this immediatly after your insert, on the same mysql connection, you get the inserted id matching that insert statement, independantly of any other inserts going on in the mean time.如果您在插入后立即调用它,在同一个 mysql 连接上,您将获得与该插入语句匹配的插入 ID,与同时进行的任何其他插入无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.