Reputation: 57
In my script a value is added up until its capacity is reached. Then it shouldn't be increased anymore. I do it like this:
$query = "UPDATE TableOne SET amount = amount + 1 WHERE id = 1 AND amount < capacity";
The value is updated as it should and it stops being raised when the capacity is reached. Then I wanted to check if the query was successful or not, I tried it with if(mysqli_query($con,$query))
, but it always returns true, even when the amount has reached the capacity and no value is changed in the table. When this condition is wrong, shouldn't the whole query be wrong then, or how do I check if it is true or not?
For the context the part of the script:
<?php
$con = new mysqli($server, $username, $pass, $db);
$query = "UPDATE TableOne SET amount = amount + 1 WHERE id = 1 AND amount < capacity";
if(mysqli_query($con,$query)){
echo 'true';
} else {
echo 'false';
}
mysqli_close($con);
?>
Upvotes: 1
Views: 900
Reputation: 38502
Try to debug by printf()
, how may records are affected by your update query like this. See mysqli_affected_rows
printf("Affected rows (UPDATE): %d\n", mysqli_affected_rows($con));
For your case something like this,
mysqli_query($con,$query);
if(mysqli_affected_rows($con) > 0){
echo 'true';
} else {
echo 'false';
}
Upvotes: 1