Chris Chris - 5 months ago 42
SQL Question

PDO mysql: How to know if insert was successful

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

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