【问题标题】:Issue with PDO lastinsertidPDO lastinsertid 的问题
【发布时间】:2012-12-24 00:57:31
【问题描述】:

我是一名初级程序员,正在对数据库应用程序进行非常简单的插入操作。

这是我的代码:

$hostname = 'localhost';
$username = '***********';
$password = '**********';
$conn = new PDO("mysql:host=$hostname;dbname=***********", $username, $password);
$sql = ("INSERT INTO ******** (name, date_entered) VALUES (?, ?)");
$q = $conn->prepare($sql);
$q->execute(array($name, date("Y-m-d")));

var_dump($q); // Trying to figure out what the issue was
echo $sql->lastInsertId(); 

插入工作正常,但我无法获得 lastInsertId 的任何值。为什么? var_dump($q)的结果:

object(PDOStatement)#4662 (1) { ["queryString"]=> string(54) "INSERT INTO ******** (name, date_entered) VALUES (?, ?)" }

感谢您的任何帮助!非常感谢!

【问题讨论】:

  • 开启error_reporting,因为你会得到类似Trying to get property on a non-object at line xerror_reporting(E_ALL); ini_set('display_errors', 1);的东西

标签: php mysql pdo lastinsertid


【解决方案1】:

您正在尝试对字符串对象执行 lastInsertId 函数;试试这个:

echo $conn->lastInsertId(); 

【讨论】:

    猜你喜欢
    • 2011-02-10
    • 2012-07-09
    • 2020-01-01
    • 2010-09-22
    • 2012-06-07
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多