本文介紹了PDO 最后插入 ID 總是正確的嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有以下代碼:
<?
$query =$db->prepare("INSERT INTO a_table (id, a_field) VALUES ('', (:a_field)");
$query->bindParam(":a_field", $a_value);
$query->execute();
$last_id = $db->lastInsertId('a_table');
?>
我想問的是這個.想象一下,當兩個人完全同時加載頁面時,是否有可能在檢索到最后一個 ID 之前插入其他人的查詢從而混淆 ID 的危險?
What I want to ask is this. Imagine when two people load the page at exactly the same time, is it a possible danger that the other persons query is inserted before the last ID is retrieved, mixing up the IDs?
推薦答案
不,這種情況是不可能的.方法 $db->lastInsertId() 返回此數據庫連接的最后插入的 id.在其他頁面將是另一個連接和另一個最后插入的 id.
No, this situation is impossible. Method $db->lastInsertId() returns last inserted id for this DB conection. In other page will be another connection and another last inserted id.
這篇關于PDO 最后插入 ID 總是正確的嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!