16

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.

Adam Lear
  • 35,439
  • 12
  • 80
  • 98
Marnix
  • 273
  • 1
  • 3
  • 13

3 Answers3

33

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();

Jarrod Dixon
  • 15,346
  • 9
  • 57
  • 72
Damon Skelhorn
  • 1,431
  • 11
  • 17
15

lastInsertId is a method of PDO, not PDOStatement. Therefore:

$db->lastInsertId();
deceze
  • 471,072
  • 76
  • 664
  • 811
  • Then I'm getting the error: Call to undefined method database::lastInsertId() – Marnix Oct 19 '12 at 08:55
  • Does `database` extend `PDO`? What PHP version are you on? – deceze Oct 19 '12 at 08:57
  • My PHP version is: 5.2.12. I have placed my PHP database class in the begin post. – Marnix Oct 19 '12 at 08:59
  • Well, since `database` doesn't extend `PDO`, it obviously doesn't have a method `lastInsertId` either. You need to pass that call through to your `$handleDB` `PDO` object, or you need to return that object so your code can call `lastInsertId` on it. – deceze Oct 19 '12 at 09:05
0

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.

Pedro del Sol
  • 2,774
  • 9
  • 40
  • 49