PDO LastInsertId 问题,php

发布于 2024-08-29 17:25:55 字数 884 浏览 2 评论 0原文

我已经尝试了很多方法来使用下面的代码(来自较大类的代码片段)获取最后插入的 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;

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.

Does anyone know howto get PDO lastInsertId to work?

Thanks in advance.

    $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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上得阳光 2024-09-05 17:25:55

在您的代码片段中,我看到一些可能对问题产生影响的细微不一致。例如,在准备您使用的 SQL 语句的代码中,

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

请注意 $this 关键字。然后,要检索 ID,您可以调用:

$dbh->lastInsertId();

您是否尝试过使用

$this->dbh->lastInsertId();

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); 

Notice the $this keyword. Then to retrieve the ID, you call,

$dbh->lastInsertId();

Have you tried using,

$this->dbh->lastInsertId();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文