PDO lastInsertId problems, php

I tried many ways to get the last inserted id with the code below (snipplet from a larger class), and now I refused.

Does anyone know how to make PDO lastInsertId work?

Thanks in advance.

$sql = "INSERT INTO auth (surname, forename, email, mobile, mobilepin, actlink, regdate) VALUES (:surname, :forename, :email, :mobile, :mobpin, :actlink, NOW())"; $stmt = $this->dbh->prepare($sql); if(!$stmt) { return "st"; } $stmt->bindParam(':surname', $this->surname); $stmt->bindParam(':forename', $this->forename); $stmt->bindParam(':email', $this->email); $stmt->bindParam(':mobile', $this->mobile); $stmt->bindParam(':mobpin', $this->mobilePin); $stmt->bindParam(':actlink', $this->actlink); $result = $stmt->execute(); //return var_dump($result); $arr = array(); $arr = $stmt->errorInfo(); $_SESSION['record'] = 'OK' . $dbh->lastInsertId(); $arr .= $_SESSION['record']; return $arr; 
+7
php pdo
source share
1 answer

In your code snippet, I saw some minor inconsistencies that could affect the problem. For example, in the code to prepare your SQL statement that you are using,

 $stmt = $this->dbh->prepare($sql); 

Pay attention to the keyword $this . Then, to get the identifier, you call

 $dbh->lastInsertId(); 

Have you tried using

$this->dbh->lastInsertId();

+12
source share

All Articles