本文介紹了PDO lastInsertId 問題,php的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我已經(jīng)嘗試了很多方法來使用下面的代碼(來自較大班級的片段)獲取最后插入的 ID,現(xiàn)在我已經(jīng)放棄了.
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;
推薦答案
在您的代碼片段中,我看到了一些可能對問題產(chǎn)生影響的輕微不一致.例如,在準(zhǔn)備您使用的 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
關(guān)鍵字.然后要檢索 ID,您調(diào)用,
Notice the $this
keyword. Then to retrieve the ID, you call,
$dbh->lastInsertId();
你有沒有試過使用,
$this->dbh->lastInsertId();
這篇關(guān)于PDO lastInsertId 問題,php的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!