我是一个初学者程序员,正在做一个相当简单的数据库应用程序插入。
下面是我的代码:
$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 (?, ?)" }感谢大家的帮助!非常感谢!
发布于 2012-12-24 09:02:20
您正在尝试对字符串对象执行lastInsertId函数;请尝试以下方法:
echo $conn->lastInsertId(); https://stackoverflow.com/questions/14015984
复制相似问题