本文介紹了PDO lastInsertId 問題,php的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我已經嘗試了很多方法來使用下面的代碼(來自較大班級的片段)獲取最后插入的 ID,現在我已經放棄了.
I have tried lots of ways to get the last inserted ID with the code below (snipplet from larger class) and now I have given up.
有誰知道如何讓 PDO lastInsertId 工作?
Does anyone know howto get PDO lastInsertId to work?
提前致謝.
$sql = "INSERT INTO auth (surname, forename, email, mobile, mobilepin, actlink, regdate) VALUES (:surname, :forename, :email, :mobile, :mobpin, :actlink, NOW())";
$stmt = $this->dbh->prepare($sql);
if(!$stmt) {
return "st";
}
$stmt->bindParam(':surname', $this->surname);
$stmt->bindParam(':forename', $this->forename);
$stmt->bindParam(':email', $this->email);
$stmt->bindParam(':mobile', $this->mobile);
$stmt->bindParam(':mobpin', $this->mobilePin);
$stmt->bindParam(':actlink', $this->actlink);
$result = $stmt->execute();
//return var_dump($result);
$arr = array();
$arr = $stmt->errorInfo();
$_SESSION['record'] = 'OK' . $dbh->lastInsertId();
$arr .= $_SESSION['record'];
return $arr;
推薦答案
在您的代碼片段中,我看到了一些可能對問題產生影響的輕微不一致.例如,在準備您使用的 SQL 語句的代碼中,
In your code snippet, I saw some minor inconsistencies that may have an effect on the problem. For an example, in the code to prepare your SQL statement you use,
$stmt = $this->dbh->prepare($sql);
注意 $this
關鍵字.然后要檢索 ID,您調用,
Notice the $this
keyword. Then to retrieve the ID, you call,
$dbh->lastInsertId();
你有沒有試過使用,
$this->dbh->lastInsertId();
這篇關于PDO lastInsertId 問題,php的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!