問題描述
我的問題很簡(jiǎn)單.我讀過,檢索我插入到 mysqli 中的行的 auto_increment/id 值的推薦方法是 mysqli_insert_id()
函數(shù).但是,我不太熟悉,有一個(gè)問題:(此時(shí)這都是理論上的)
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 應(yīng)用程序.假設(shè)多個(gè)用戶同時(shí)在應(yīng)用程序上,并且在我插入我的行和我調(diào)用 mysqli_insert_id()
的時(shí)間之間從不同的頁面插入另一行?
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()
?
這會(huì)返回錯(cuò)誤的值,還是 MySQL 有某種功能可以防止這種情況發(fā)生?還是我只是高估了這種情況的可能性?
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?
感謝您的時(shí)間.
推薦答案
mysqli_insert_id()
特定于數(shù)據(jù)庫連接——它返回this 最近插入的腳本調(diào)用,不是任何其他 MySQL 客戶端.因此,如果多個(gè)應(yīng)用程序同時(shí)插入到數(shù)據(jù)庫中,則不會(huì)發(fā)生沖突.
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.
這篇關(guān)于mysqli_insert_id:如果有人在我調(diào)用它之前插入另一行怎么辦?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!