PDO mysql: How to know if insert was successful

Chris picture Chris · Nov 2, 2009 · Viewed 114k times · Source

I'm using PDO to insert a record (mysql and php)

$stmt->bindParam(':field1', $field1, PDO::PARAM_STR);
$stmt->bindParam(':field2', $field2, PDO::PARAM_STR);
$stmt->execute();

Is there a way to know if it inserted successfully, for example if the record was not inserted because it was a duplicate?

Edit: of course I can look at the database, but I mean programmatic feedback.

Answer

Ólafur Waage picture Ólafur Waage · Nov 2, 2009

PDOStatement->execute() returns true on success. There is also PDOStatement->errorCode() which you can check for errors.