Page 1 of 1
PDO: lastInsertID() not working
Posted: Wed Aug 13, 2014 6:10 am
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?
Re: PDO: lastInsertID() not working
Posted: Wed Aug 13, 2014 6:19 am
by jdhmtl
You're calling it on the PDOStatement. You need to call it on the PDO object itself.
Re: PDO: lastInsertID() not working
Posted: Wed Aug 13, 2014 6:26 am
by simonmlewis
How? Where?
You do it like this in mysql, but not in PDO. Not familiar with it in PDO.
Re: PDO: lastInsertID() not working
Posted: Wed Aug 13, 2014 6:30 am
by jdhmtl
Re: PDO: lastInsertID() not working
Posted: Wed Aug 13, 2014 6:32 am
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.
Re: PDO: lastInsertID() not working
Posted: Wed Aug 13, 2014 6:43 am
by simonmlewis
Oh I see what you mean now. Thanks.