即使实际发生更新,mysql_affected_rows()的UPDATE语句仍返回0 [英] mysql_affected_rows() returns 0 for UPDATE statement even when an update actually happens

查看:680
本文介绍了即使实际发生更新,mysql_affected_rows()的UPDATE语句仍返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个简单的mysql更新查询中获取受影响的行数.但是,当我在下面运行此代码时,PHP的mysql_affected_rows()始终等于0.无论是否foo = 1已经存在(在这种情况下,函数应该正确返回0,因为未更改任何行),或者如果foo当前等于其他整数(在这种情况下,该函数应返回1).

I am trying to get the number of rows affected in a simple mysql update query. However, when I run this code below, PHP's mysql_affected_rows() always equals 0. No matter if foo=1 already (in which case the function should correctly return 0, since no rows were changed), or if foo currently equals some other integer (in which case the function should return 1).

$updateQuery = "UPDATE myTable SET foo=1 WHERE bar=2";
mysql_query($updateQuery);
if (mysql_affected_rows() > 0) {
    echo "affected!";
}
else {
    echo "not affected"; // always prints not affected
}

UPDATE语句本身可以工作. INT在我的数据库中被更改.我还仔细检查了数据库连接是否未事先关闭或任何时髦.请记住,尽管我也尝试过mysql_affected_rows不一定要求您传递连接链接标识符.

The UPDATE statement itself works. The INT gets changed in my database. I have also double-checked that the database connection isn't being closed beforehand or anything funky. Keep in mind, mysql_affected_rows doesn't necessarily require you to pass a connection link identifier, though I've tried that too.

有关函数的详细信息: mysql_affected_rows

Details on the function: mysql_affected_rows

有什么想法吗?

推荐答案

MySQL的较新版本足够聪明,可以查看是否进行了修改.假设您启动了UPDATE语句:

Newer versions of MySQL are clever enough to see if modification is done or not. Lets say you fired up an UPDATE Statement:

UPDATE tb_Employee_Stats SET lazy = 1 WHERE ep_id = 1234

让我们说一下列的值是否已经为1;那么将不会发生更新过程,因此 mysql_affected_rows()将返回0;否则,将返回0.否则,如果lazy列具有其他值而不是1,则返回1.除了人为错误之外,没有其他可能性.

Lets say if the Column's Value is already 1; then no update process occurs thus mysql_affected_rows() will return 0; else if Column lazy had some other value rather than 1, then 1 is returned. There is no other possibilities except for human errors.

这篇关于即使实际发生更新,mysql_affected_rows()的UPDATE语句仍返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆