本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!