2011-12-20 285 views
11

我試圖通過其他一些帖子看,但沒有看到任何確切的東西,我正在尋找。PHP MySQL PDO lastInsertID導致致命錯誤

我有一個DB查詢

$sql = "INSERT INTO groups(Name) VALUES (:name)"; 
$dbs = $dbo->prepare($sql); 

$dbs->bindParam(":name", $_POST['name'], PDO::PARAM_STR); 

$dbs->execute(); 

$groupID = $dbs->lastInsertId(); 

返回這個致命的錯誤:

[Tue Dec 20 13:59:23 2011] [error] [client 127.0.0.1] PHP Fatal error: Call to undefined method PDOStatement::lastInsertId() in /media/Storage/www/2011/admin/public/ajax.users.php on line 87, referer: http://localhost/2011/admin/public/menu.php?page=users 

按照PHP手冊PDO :: lastInsertId():

If the PDO driver does not support this capability, PDO::lastInsertId() triggers an IM001 SQLSTATE.

如何我會確定我的服務器是否支持lastInsertId()?我在任何地方的錯誤日誌中都沒有看到IM001。

當我運行這個時,數據插入正常,但我無法得到它的ID用於設置組權限的下一組INSERT中。

回答

28

lastInsertId()是PDO類的一個方法,而不是PDOStatement類。

這應該工作:

$groupID = $dbo->lastInsertId(); 
+2

LOL哇,我覺得愚蠢的感謝! – guyfromfl 2011-12-20 19:22:16