It seems that for an INSERT statement, one can use if (isset($connect->lastInsertId()))
in order to check whether the INSERT statement was successful. (Pleas
$stmt->execute();
$count = $stmt->rowCount();
if($count =='0'){
echo "Failed !";
}
else{
echo "Success !";
}
It depends on what you mean by "successful." If you mean that the query executed without failing, then PDO
will either throw an exception on failure or return FALSE
from PDOStatement::execute()
, depending on what error mode you have set, so a "successful" query in that case would just be one in which the execute method did not return FALSE
or throw an exception.
If you mean "successful" in that there were actually rows updated (versus just 0 rows updated), then you'd need to check that using PDOStatement::rowCount(), which will tell you the number of affected rows from the previous query.
Warning: For updates where newvalue = oldvalue
PDOStatement::rowCount()
returns zero. You can use
$p = new PDO($dsn, $u, $p, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));
in order to disable this unexpected behaviour.