What is a sure way to tell if an update query succeeded when using php pdo and mysql?
In my app, I update totals of items that are submitted but a user, and the table looks like this:
items
userId | itemsAdded | itemsChecked | itemsUnChecked | itemsTotal
1 | 5 | 2 | 3 | 5
So when I do update items set itemTotals = itemsChecked+itemUnChecked
the itemsTotal
column remains the same unless the itemsAdded changes and the itemsUnChecked increments (2 + 3 equals 5, 1 + 4 is also 5).
I used to use rowCount() to check if a query succeeded, but in this case, since the itemsTotal
column stays the same, there's no way of telling if the sql succeeded or not.
$query = $conn->prepare($sql);
$query->execute(array(
":itemCount" => $itemCount
":itemId" => $itemId
));
$queryCount = $query->rowCount();
if($queryCount == 1) {
echo 'Updated succeeded';
} else {
echo 'Updated failed!';
}
I could also use:
$query = $conn->prepare($sql);
$result = $query->execute(array(
":itemCount" => $itemCount
":itemId" => $itemId
));
if($result) {
echo 'Updated succeeded';
} else {
echo 'Updated failed!';
}
But does that return true or false based on if the query succeed or based on the number of rows it updated?
I only need to check if the query succeeded or not. No need to tell the number of rows that were updated.