問題描述
我有一個(gè) mysql 問題,mysqli_insert_id()
返回來自整個(gè)服務(wù)器或插入該 ID 的特定用戶的記錄?因?yàn)槿绻鼜恼w返回那么使用這個(gè)功能將不安全
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 的評(píng)論完全錯(cuò)誤.insert_id()
返回放入執(zhí)行 insert_id() 查詢的連接執(zhí)行的最后一個(gè) 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,它不返回某個(gè) OTHER 連接創(chuàng)建的 id,它不返回某個(gè) OTHER 用戶創(chuàng)建的 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.
它實(shí)際上是由 LAST 插入 YOU 創(chuàng)建的最后一個(gè) ID YOU.
It is literally the last ID YOU created by the LAST insert YOU performed.
這意味著執(zhí)行插入是 100% 可靠的,通過 last_insert_id()
獲取創(chuàng)建的 ID,然后在其他查詢中使用該 ID 創(chuàng)建父/子記錄.
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.
但請(qǐng)注意,insert_id()
只會(huì)返回 ONE id 值.如果您進(jìn)行多值插入,您仍然只能從最后一個(gè)值集中獲取 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)
仍然在內(nèi)部執(zhí)行兩次插入,您只能獲得 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
.
同樣,之前的所有查詢都沒有內(nèi)存:
As well, there's no memory for all previous queries:
INSERT ... // query #1
INSERT ... // query #2
SET id = last_insert_id();
只會(huì)得到查詢#2創(chuàng)建的ID,查詢#1的ID丟失"了.
will only get the ID created by query #2, and the ID from query #1 is "lost".
這篇關(guān)于mysqli_insert_id() 是從整個(gè)服務(wù)器還是從同一用戶獲取記錄?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!