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

查看:18
本文介绍了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.

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

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() 只会返回 ONE 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天全站免登陆