开发者

Why does mysql_affected_rows return 0 even if one record should be updated

mysql_query("update users set balance=balance+'$pwbalance'开发者_运维百科-'$totalprice' where memberid='$memberid' and (balance+'$pwbalance'-'$totalprice')>=0")or die(mysql_error());
$count=mysql_affected_rows();

When I echo $pwbalance, it is 40.00; when I echo $totalprice, it is 40; So there should be one record to be updated. However, when I echo $count, I get 0. What's wrong?


MySQL only actually updates a row if there would be a noticeable difference before and after the updat. Your calculation is basically:

SET balance = balance + 40 - 40

So nothing changes, and MySQL will not count this as an affectd row.

Side note: don't single quote numeric values in the sql. single quotes act as string delimiters. For mysql , in this case, they are automatically converted to numbers, but it is bad practice at any rate.


Roland is right, obviously you want to know if the query was successful, the fact that the balance stays the same is unimportant for you. You might want to add a new field like purchase_count that you increment in this update, or last_purchase_date, something to trigger an actual update.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜