简体   繁体   English

PDO lastInsertId问题,PHP

[英]PDO lastInsertId issues, php

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. 我已经尝试了许多方法来获取下面代码(较大类的摘要)中最后插入的ID,但现在我放弃了。

Does anyone know howto get PDO lastInsertId to work? 有谁知道如何让PDO lastInsertId起作用?

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;

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, 例如,在准备使用的SQL语句的代码中,

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

Notice the $this keyword. 注意$this关键字。 Then to retrieve the ID, you call, 然后要获取ID,您可以调用,

$dbh->lastInsertId();

Have you tried using, 您是否尝试过使用

$this->dbh->lastInsertId();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM