mysqli_insert_id()是从整个服务器还是从同一用户获取记录? [英] Is mysqli_insert_id() gets record from whole server or from same user?

查看:30
本文介绍了mysqli_insert_id()是从整个服务器还是从同一用户获取记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有mysql问题,mysqli_insert_id()从整个服务器或插入该ID的特定用户返回记录?因为如果它从总体上返回,那么使用此函数将是不安全的

I have mysql question that mysqli_insert_id() returns record from overall server or from specific user who has inserted that id? Because if it returns from overall then it won't be secure to use this function

推荐答案

上面@daan的评论完全是错误的. insert_id()返回放置在执行插入insert(id)查询的连接所执行的最后一个insert查询的auto_increment字段中的ID.

The comment from @daan above is flat-out wrong. insert_id() returns the ID placed into an auto_increment field of the last insert query that the connection executing the insert_id() query performed.

它不返回表中最大的ID,不返回由OTHER连接创建的ID,不返回由OTHER用户创建的ID.

It does not return the largest ID in the table, it doesn't return the id created by some OTHER connection, it doesn't return the id created by some OTHER user.

它实际上是由 LAST 插入 YOU 执行的最后一个ID YOU .

It is literally the last ID YOU created by the LAST insert YOU performed.

这意味着执行插入操作,通过last_insert_id()获取创建的ID,然后在其他查询中使用该ID创建父/子记录是100%可靠的.

That means it is 100% reliable to perform an insert, get the created ID via last_insert_id() and then use that ID in other queries to create parent/child records.

但是请注意,insert_id()仅会返回一个 id值.如果您进行多值插入,则仍然只能从上一个值集(例如

But note that insert_id() only ever returns ONE id value. If you do a multi-value insert, you still only get the ID from the last value set, e.g.

INSERT INTO sometable (x, y) VALUES (1,2), (2,3)

仍在内部执行两次插入,并且您只会获得2,3元组的ID,而不是1,2.

still performs two inserts internally, and you'd only get the id for the 2,3 tuple, not the 1,2.

同样,所有以前的查询都没有存储空间:

As well, there's no memory for all previous queries:

INSERT ...   // query #1
INSERT ...   // query #2
SET id = last_insert_id();

只会获取查询2创建的ID,而查询1的ID为丢失".

will only get the ID created by query #2, and the ID from query #1 is "lost".

这篇关于mysqli_insert_id()是从整个服务器还是从同一用户获取记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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