問題描述
我有一個 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() 是從整個服務器還是從同一用戶獲取記錄?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!