我已经尝试了很多方法来获得最后插入的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;发布于 2010-04-20 22:12:11
在您的代码片段中,我看到了一些可能会对问题产生影响的细微不一致。例如,在用于准备SQL语句的代码中,
$stmt = $this->dbh->prepare($sql); 请注意$this关键字。然后,要检索ID,您可以调用
$dbh->lastInsertId();你有没有试过使用
$this->dbh->lastInsertId();
https://stackoverflow.com/questions/2675766
复制相似问题