mysql_insert_id使用安全吗? [英] Is mysql_insert_id safe to use?

查看:48
本文介绍了mysql_insert_id使用安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据PHP文档,mysql_insert_id从mysql表中获取最后插入的ID.

According to the PHP documentation, mysql_insert_id takes the last inserted id from the mysql table.

我的问题是,如果我有一个网站每秒向数据库插入多于2行,我是否可以使用mysql_insert_id并获得之前在INSERT查询行中引用的正确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?

推荐答案

摘自MySQL手册:

生成的ID会在每个连接的基础上保留在服务器中.这意味着函数返回给定客户端的值是为该客户端影响AUTO_INCREMENT列的最新语句生成的第一个AUTO_INCREMENT值.即使其他客户端生成自己的AUTO_INCREMENT值,该值也不会受到其他客户端的影响.这种行为可确保每个客户端都可以检索自己的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.

简短版本:使用安全.

这篇关于mysql_insert_id使用安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆