問題描述
我的問題很簡單.我讀過,檢索我插入到 mysqli 中的行的 auto_increment/id 值的推薦方法是 mysqli_insert_id()
函數.但是,我不太熟悉,有一個問題:(此時這都是理論上的)
My question is a rather simple one. I've read that the recommended method of retrieving the auto_increment/id value of a row I've inserted in mysqli is the mysqli_insert_id()
function. However, I'm not too familiar and had a question: (This is all theoretical at this point)
出于這些目的(因此是 mysqli 位),這一切都將來自 PHP Web 應用程序.假設多個用戶同時在應用程序上,并且在我插入我的行和我調用 mysqli_insert_id()
的時間之間從不同的頁面插入另一行?
For these purposes (hence the mysqli bit) this is all going to be from a PHP web application. Say multiple users are on the application at once and another row is inserted from a different page between the time that I insert my row and the time that I call mysqli_insert_id()
?
這會返回錯誤的值,還是 MySQL 有某種功能可以防止這種情況發生?還是我只是高估了這種情況的可能性?
Would that return an incorrect value, or does MySQL have some sort of feature to prevent such a thing? Or am I simply overestimating the possibility of such a scenario?
感謝您的時間.
推薦答案
mysqli_insert_id()
特定于數據庫連接——它返回this 最近插入的腳本調用,不是任何其他 MySQL 客戶端.因此,如果多個應用程序同時插入到數據庫中,則不會發生沖突.
mysqli_insert_id()
is specific to the database connection -- it returns the ID of the row that this script invocation inserted most recently, not any other MySQL client. So there's no conflict if multiple applications are inserting into the database at the same time.
這篇關于mysqli_insert_id:如果有人在我調用它之前插入另一行怎么辦?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!