Thursday 9 July 2009

PDO lastInsertId issue

OK, so i may be dimmer than most, but I spent over an hour pulling my hair out over this.
To get the correct id back from PDO::lastInsertId(), you have to call it before the commit.
e.g. somthing like...

$dbh = new PDO();
$dbh->beginTransaction();
$stmt = $dbh->prepare($sql);
$stmt->execute($params);
$return = $dbh->lastInsertId();
$dbh->commit();


...will work nicely

No comments: