簡體   English   中英

PDO lastInsertId問題,PHP

[英]PDO lastInsertId issues, php

我已經嘗試了許多方法來獲取下面代碼(較大類的摘要)中最后插入的ID,但現在我放棄了。

有誰知道如何讓PDO lastInsertId起作用?

提前致謝。

    $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語句的代碼中,

$stmt = $this->dbh->prepare($sql); 

注意$this關鍵字。 然后要獲取ID,您可以調用,

$dbh->lastInsertId();

您是否嘗試過使用

$this->dbh->lastInsertId();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM