PDO: lastInsertID() not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

PDO: lastInsertID() not working

Post by simonmlewis »

Code: Select all

  $query = "INSERT INTO admin (firstname, lastname, phone, postcode, email, password, hintquestion, hintanswer, usertype, status) VALUES (:firstname, :lastname, :phone, :postcode, :email, :password, :hintquestion, :hintanswer, :usertype, :status)";
$usertype = "customer";
$status = "live";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':firstname' => $firstname, ':lastname' => $lastname, ':phone' => $phone, ':postcode' => $postcode, ':email' => $email, ':password' => $password, ':hintquestion' => $hintquestion, ':hintanswer' => $hintanswer, ':usertype' => $usertype, ':status' => $status));
$newid = $stmt->lastInsertId();
[text]Fatal error: Call to undefined method PDOStatement::lastInsertId() in C:\xampp\phpMyAdmin\site\includes\a_register.inc on line 91[/text]
What have I missed?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
jdhmtl
Forum Newbie
Posts: 18
Joined: Tue Aug 12, 2014 7:33 am

Re: PDO: lastInsertID() not working

Post by jdhmtl »

You're calling it on the PDOStatement. You need to call it on the PDO object itself.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PDO: lastInsertID() not working

Post by simonmlewis »

How? Where?
You do it like this in mysql, but not in PDO. Not familiar with it in PDO.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
jdhmtl
Forum Newbie
Posts: 18
Joined: Tue Aug 12, 2014 7:33 am

Re: PDO: lastInsertID() not working

Post by jdhmtl »

Code: Select all

$pdo->lastInsertId();
jdhmtl
Forum Newbie
Posts: 18
Joined: Tue Aug 12, 2014 7:33 am

Re: PDO: lastInsertID() not working

Post by jdhmtl »

When you call 'new PDO()', you're creating a PDO object. Calling its query() or prepare() methods returns a PDOStatement object. The PDO manual shows return types and which methods are available to which object.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PDO: lastInsertID() not working

Post by simonmlewis »

Oh I see what you mean now. Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply