php 未定义的方法 PDO lastInsertId
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12970626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
undefined method PDO lastInsertId
提问by Marnix
I have an insert query, and I want to get the ID from the table. I have been searching, and I found lastInsertId() for PDO. When I want to use it, I get PHP errors.
我有一个插入查询,我想从表中获取 ID。我一直在搜索,我找到了 PDO 的 lastInsertId()。当我想使用它时,出现 PHP 错误。
This is my code:
这是我的代码:
$db = new database();
$naam = $db->quoteQuery($_POST['naam']);
$barcode = $db->quoteQuery($_POST['barcode']);
$sql = "INSERT INTO products(name, barcode) VALUES (".$name.",".$barcode.")";
$results = $db->executeQuery($sql);
$lastid = $results->lastInsertId();
But this gives an error, this one:
但这给出了一个错误,这个:
Fatal error: Call to undefined method PDOStatement::lastInsertId() in /home/onlineweuh/domains/onlinewebapps.nl/public_html/vsb/admin/add-product.class.php on line 297
My database class:
我的数据库类:
class database
{
private $handleDB;
public function __construct()
{
$host = ;
$user = ;
$database = ;
$password = ;
try
{
$this->handleDB = new PDO('mysql:host='.$host.';dbname='.$database, $user, $password);
}
catch (PDOException $e)
{
print_r($e);
}
$this->handleDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
I hope someone can help me solve it, I want the ID which is given at the insert Query.
我希望有人能帮我解决它,我想要在插入查询中给出的 ID。
回答by Damon Skelhorn
You get the lastinsertid from the PDO object and not your results object.
您从 PDO 对象而不是您的结果对象获得 lastinsertid。
Try $db->lastInsertId()
尝试 $db->lastInsertId()
edit below.
下面编辑。
Your database class is encapsulating your handleDB / PDO object. Since the handleDB variable is private, you cannot access this outside your class. You would need to either make it public like so;
您的数据库类正在封装您的 handleDB / PDO 对象。由于 handleDB 变量是私有的,因此您无法在类之外访问它。您需要像这样将其公开;
class database
{
public $handleDB;
public function __construct()
{
$host = 'removed';
$user = 'removed';
$database = 'removed';
$password = 'removed';
try
{
$this->handleDB = new PDO('mysql:host='.$host.';dbname='.$database, $user, $password);
}
catch (PDOException $e)
{
print_r($e);
}
$this->handleDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
}
Now you can call $db->handleDB->lastInsertId();
现在你可以打电话 $db->handleDB->lastInsertId();
Or you could expose the handleDB->lastInsertId()as a function like:
或者您可以将 公开handleDB->lastInsertId()为一个函数,例如:
class database
{
private $handleDB;
public function __construct()
{
$host = 'remove';
$user = 'removed';
$database = 'removed';
$password = 'removed';
try
{
$this->handleDB = new PDO('mysql:host='.$host.';dbname='.$database, $user, $password);
}
catch (PDOException $e)
{
print_r($e);
}
$this->handleDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
public function lastInsertId(){
return $this->handleDB->lastInsertId();
}
}
You would call using $db->lastInsertId();
你会打电话使用 $db->lastInsertId();
回答by deceze
lastInsertIdis a method of PDO, not PDOStatement. Therefore:
lastInsertId是 的一种方法PDO,不是PDOStatement。所以:
$db->lastInsertId();
回答by Pedro del Sol
your database class needs to be a subclass of PDO by extending PDO
您的数据库类需要通过扩展 PDO 成为 PDO 的子类
class database extends PDO
that way all the methods in PDO are available to your subclass.
这样 PDO 中的所有方法都可用于您的子类。

