2012-12-24 68 views
1

我是一个初学者程序员,并正在一个非常简单的插入到数据库应用程序。问题与PDO lastinsertid

这里是我的代码:

$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 (?, ?)" } 

感谢您的任何和所有帮助!非常感谢!

+0

打开,因为你会得到像'试图以线非对象x''的error_reporting(E_ALL)上获得的财产; ini_set('display_errors',1);' –

回答

3

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

echo $conn->lastInsertId();