問題描述
我正在使用 PDO 做一個帶有 INSERT
SQL 查詢的注冊表單.在此 INSERT 之后,我想提取剛剛創(chuàng)建的 userid
(自動遞增,主鍵),并將其 INSERT
提取到另一個表(確認(rèn)代碼"表)中
I am doing a signup form with an INSERT
SQL query using PDO. After this INSERT, I want to extract the userid
(auto incrementing, primary key) that was just created, and INSERT
it into another table (a "confirmation code" table)
但我如何確保此用戶 ID 不是第二個用戶的用戶 ID,該用戶 ID 比第一個用戶晚 1/1000 秒注冊?
But how can I make sure that this userid is not that of a second user who registered 1/1000th of a second later that the first user?
我應(yīng)該找到某種方法來鎖定桌子嗎?我應(yīng)該使用事務(wù)嗎?
Should I find some way of locking the table? Should I use transactions?
推薦答案
lastInsertId()
返回在該連接上插入的最后一行的標(biāo)識符,因此并發(fā)用戶(與數(shù)據(jù)庫的不同連接)不會干擾.
lastInsertId()
returns the identifier of the last row inserted on that connection, so concurrent users (with different connections to the database) will not interfere.
這篇關(guān)于如何確保 PDO 的 lastInsertId() 不是另一個同時插入的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!