可能@mysql_affected_rows 返回值? [英] Possibilites @mysql_affected_rows return Values?
问题描述
谁能告诉我@mysql_affected_rows 返回值的所有可能性.因为我正在检查 if(@mysql_affected_rows())
.在大多数情况下,它返回 1(成功)或 0(不成功).所以它工作正常.但在某些情况下,如果更新不成功,它会返回一些非 0 的值.
Can anyone please tell me what are all the possibilities of return values for @mysql_affected_rows. Because i am checking if(@mysql_affected_rows())
. In most case it returns 1(sucess) or 0(not sucess). So it worked correctly. But in some cases it returns some value other than 0 if not success on updation.
我不知道它返回什么.但它进入了循环.在循环内部,我试图插入数据.但它显示了 SQL 上的重复错误.供您参考,请检查以下代码,
I dont know what it returns. But it come into the loop. Inside loop i am trying to insert the data. But it shows duplication error on SQL for that. For your reference check the below code please,
$sqlU = sprintf("UPDATE %s SET count = count + 1
WHERE id = %d", 'table', 123);
mysql_query($sqlU);
if(!@mysql_affected_rows()) {
$sqlI = sprintf("INSERT INTO %s (id) VALUES (%d)",
'table', 123);
mysql_query($sqlI);
}
推荐答案
首先,不推荐使用 mysql_*
函数.mysql_affected_rows()
似乎在所有情况下都返回一个整数.如果查询失败,它返回 -1
.
First, the mysql_*
functions are deprecated. mysql_affected_rows()
seems to return an integer in all cases. It returns -1
if the query failed.
你真正需要的不是 mysql_affected_rows()
是一个对竞争条件不开放的正确查询.
What you really need instead of mysql_affected_rows()
is a proper query that is not open to race conditions.
INSERT INTO table (id, count) VALUES (123, 0) ON DUPLICATE KEY UPDATE count = count + 1
这篇关于可能@mysql_affected_rows 返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!