PHP MySQL PDO lastInsertID导致致命错误

时间:2020-12-13 19:27:06

I tried looking through some other posts, but didn't see anything exactly what I'm looking for.

我尝试查看其他一些帖子,但没有看到任何我正在寻找的内容。

I have a DB query

我有一个数据库查询

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

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

$dbs->execute();

$groupID = $dbs->lastInsertId();

That returns this fatal error:

这会返回这个致命的错误:

[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

According to the php manual for PDO::lastInsertId():

根据PDO :: lastInsertId()的php手册:

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

如果PDO驱动程序不支持此功能,PDO :: lastInsertId()将触发IM001 SQLSTATE。

How would I determine if my server supports lastInsertId()? I do not see IM001 in my error log anywhere.

我如何确定我的服务器是否支持lastInsertId()?我在任何地方的错误日志中都没有看到IM001。

When I run this, the data is inserted fine, but I cannot get its ID to use in the next set of INSERT's that set the group permissions.

当我运行它时,数据插入正常,但我无法在下一组设置组权限的INSERT中使用它的ID。

1 个解决方案

#1


28  

lastInsertId() is a method of the PDO class, not the PDOStatement class.

lastInsertId()是PDO类的方法,而不是PDOStatement类。

This should work:

这应该工作:

$groupID = $dbo->lastInsertId();

#1


28  

lastInsertId() is a method of the PDO class, not the PDOStatement class.

lastInsertId()是PDO类的方法,而不是PDOStatement类。

This should work:

这应该工作:

$groupID = $dbo->lastInsertId();