问题描述
我有一个插入查询,我想从表中获取ID。我一直在搜索,我发现lastInsertId()为PDO。当我想使用它,我得到PHP错误。
这是我的代码:
$ 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();
但这会产生错误,这一个:
致命错误:调用未定义的方法PDOStatement :: lastInsertId()in /home/onlineweuh/domains/onlinewebapps.nl/public_html/vsb/admin/add-product.class.php on line 297
我的数据库类:
类数据库
{
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);
}
我希望有人能帮我解决它,我想要的ID
您从PDO对象中获取lastinsertid而不是结果对象。
请尝试 $ db-> lastInsertId()
/ p>
您的数据库类封装了您的handleDB / PDO对象。由于handleDB变量是私有的,你不能访问这个类外面。你需要像这样公开;
类数据库
{
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);
}
}
现在可以调用 $ db-> handleDB-> lastInsertId();
- > lastInsertId()作为一个函数:
类数据库
{
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();
}
}
$ c> $ db-> lastInsertId();
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.
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.
You get the lastinsertid from the PDO object and not your results object.
Try $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;
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();
Or you could expose the handleDB->lastInsertId()
as a function like:
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();
这篇关于未定义的方法PDO lastInsertId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!