問題描述
我正在使用 PDO 做一個帶有 INSERT
SQL 查詢的注冊表單.在此 INSERT 之后,我想提取剛剛創建的 userid
(自動遞增,主鍵),并將其 INSERT
提取到另一個表(確認代碼"表)中
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?
我應該找到某種方法來鎖定桌子嗎?我應該使用事務嗎?
Should I find some way of locking the table? Should I use transactions?
推薦答案
lastInsertId()
返回在該連接上插入的最后一行的標識符,因此并發用戶(與數據庫的不同連接)不會干擾.
lastInsertId()
returns the identifier of the last row inserted on that connection, so concurrent users (with different connections to the database) will not interfere.
這篇關于如何確保 PDO 的 lastInsertId() 不是另一個同時插入的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!