PDO lastInsertId issues, php(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PDO lastInsertId 问题,php
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
