Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method PDO 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.

like image 876
Marnix Avatar asked Oct 19 '12 08:10

Marnix


People also ask

What is lastInsertId?

Definition and Usage. The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.

How can I get last insert ID in PDO?

You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).


2 Answers

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

like image 106
Damon Skelhorn Avatar answered Oct 07 '22 14:10

Damon Skelhorn


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

$db->lastInsertId();
like image 32
deceze Avatar answered Oct 07 '22 16:10

deceze