简体   繁体   English

PDO lastinsertid的问题

[英]Issue with PDO lastinsertid

I am a beginner programmer and am making a pretty simple insert into database application. 我是一名初学者程序员,并且正在简单地插入数据库应用程序。

Here is my code: 这是我的代码:

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

The insert works fine, but I can't get any value of lastInsertId. 插入工作正常,但我无法获得lastInsertId的任何值。 Why? 为什么? The results of var_dump($q): var_dump($ q)的结果:

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

Thanks for any and all help! 感谢您的帮助! Its greatly appreciated! 非常感谢!

Your are trying to execute the lastInsertId-function on a string object; 您正在尝试对字符串对象执行lastInsertId函数; Try this one: 试试这个:

echo $conn->lastInsertId(); 

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

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