【问题标题】:PDO lastInsertId issues, phpPDO lastInsertId 问题,php
【发布时间】:2011-02-10 04:06:54
【问题描述】:

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

【问题讨论】:

  • 你能不能提一下它是怎么不工作的?只是说,它不起作用会导致其他人假设问题可能是什么。信息越多,答案就越多。

标签: php pdo lastinsertid


【解决方案1】:

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

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

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

$dbh->lastInsertId();

你试过用吗,

$this->dbh->lastInsertId();

【讨论】:

    猜你喜欢
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2021-12-27
    • 2020-01-01
    • 2020-10-30
    相关资源
    最近更新 更多