I'm using a typical PDO try/catch clause, and it works as it should except for one aspect: The WHERE clause.
Here is the code I have:
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
try {
$cancel_query = "UPDATE table SET active = 0 WHERE id = $request_id";
$cancel_stmt = $pdo->prepare($cancel_query);
$cancel_stmt->execute();
$message = "That request was successfully canceled.";
} catch (PDOException $e) {
$message = 'This request was not canceled. Something went wrong. ' . $e->getMessage();
}
$pdo = null;
echo $message;
This will catch the error most of the time, such as if I purposely put in the wrong table name, or column name. But if I put in a $request_id that doesn't exist, the message will indicate success. Since nothing was updated, because the WHERE clause doesn't come up with a row to update, it would be ideal in the business sense for the exception to be caught.
Is there a way for this to throw an error if no update is made?