undefined method PDO lastInsertId
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();
lastInsertId
is a method of PDO
, not PDOStatement
. Therefore:
$db->lastInsertId();
your database class needs to be a subclass of PDO by extending PDO
class database extends PDO
that way all the methods in PDO are available to your subclass.