php 即使实际发生更新,mysql_affected_rows() 也会为 UPDATE 语句返回 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9397291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:43:39  来源:igfitidea点击:

mysql_affected_rows() returns 0 for UPDATE statement even when an update actually happens

phpmysql

提问by Alex Moore

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 correctlyreturn 0, since no rows were changed), or if foo currently equals some other integer (in which case the function should return 1).

我试图在一个简单的 mysql 更新查询中获取受影响的行数。但是,当我在下面运行此代码时,PHP 的 mysql_affected_rows() 始终等于 0。无论 foo=1 是否已经(在这种情况下,函数应该正确返回 0,因为没有行被更改),或者 foo 当前是否等于某个其他整数(在这种情况下,函数应该返回 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
}

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.

UPDATE 语句本身有效。我的数据库中的 INT 发生了变化。我还仔细检查了数据库连接没有被预先关闭或任何时髦的东西。请记住,mysql_affected_rows 不一定要求您传递连接链接标识符,尽管我也尝试过。

Details on the function: mysql_affected_rows

函数详情:mysql_affected_rows

Any ideas?

有任何想法吗?

回答by Mrigesh Raj Shrestha

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

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

UPDATE tb_Employee_Stats SET lazy = 1 WHERE ep_id = 1234

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.

假设列的值是否已经为 1;然后没有更新过程发生,因此mysql_affected_rows()将返回 0;否则,如果 Column lazy 具有其他值而不是 1,则返回 1。除了人为错误,没有其他可能性。

回答by DLastCodeBender

mysqli_affected_rows requires you to pass the reference to your database connection as the only parameter, instead of the reference to your mysqli query. eg.

mysqli_affected_rows 要求您将数据库连接的引用作为唯一参数传递,而不是对 mysqli 查询的引用。例如。

$dbref=mysqli_connect("dbserver","dbusername","dbpassword","dbname");

$updateQuery = mysqli_query($dbref,"UPDATE myTable SET foo=1 WHERE bar=2");

echo mysqli_affected_rows($dbref);

NOT

不是

echo mysqli_affected_rows($updateQuery);

回答by Gladson Samuel S

The following notes will be helpful for you,

以下注意事项对您有帮助,

mysql_affected_rows() returns

mysql_affected_rows() 返回

  • +0: a row wasn't updated or inserted (likely because the row already existed, but no field values were actually changed during the UPDATE).

  • +1: a row was inserted

  • +2: a row was updated

  • -1: in case of error.

  • +0:未更新或插入行(可能是因为该行已存在,但在 UPDATE 期间实际上未更改任何字段值)。

  • +1:插入了一行

  • +2:更新了一行

  • -1:在出错的情况下。

mysqli affected rows developer notes

mysqli 受影响的行开发人员笔记

回答by steveukx

Have you tried using the MySQL function ROW_COUNTdirectly?

您是否尝试过ROW_COUNT直接使用 MySQL 功能?

mysql_query('UPDATE myTable SET foo = 1 WHERE bar = 2');
if(mysql_result(mysql_query('SELECT ROW_COUNT()'), 0, 0)) {
  print "updated";
}
else {
  print "no updates made";
}

More information on the use of ROW_COUNTand the other MySQL information functions is at: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count

有关使用ROW_COUNT和其他 MySQL 信息函数的更多信息,请访问:http: //dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count

回答by user3404872

This is because mySql is checking whether the field made any change or not, To over come this, I created a new TINY field 'DIDUPDATE' in the table.

这是因为 mySql 正在检查该字段是否进行了任何更改,为了解决这个问题,我在表中创建了一个新的 TINY 字段“DIDUPDATE”。

added this to your query 'DIDUPDATE=DIDUPDATE*-1'

将此添加到您的查询 'DIDUPDATE=DIDUPDATE*-1'

it looks like.

看起来像。

    $updateQuery = "UPDATE myTable SET foo=1, DIDUPDATE=DIDUPDATE*-1  WHERE bar=2";
    mysql_query($updateQuery);

    if (mysql_affected_rows() > 0) 
    {
            echo "affected!";
    }
    else 
    {
            echo "not affected"; 
    }

it works fine!!!

它工作正常!!!

回答by Stefan

Try connecting like this:

尝试像这样连接:

$connection = mysql_connect(...,...,...);

and then call like this

然后像这样打电话

if(mysql_affected_rows($connection) > 0)
    echo "affected";
} else { ...

回答by Marko Vasic

I think you need to try something else in update then foo=1. Put something totaly different then you wil see is it updating or not without if loop. then if it does, your if loop should work.

我认为您需要在更新中尝试其他内容,然后 foo=1。放一些完全不同的东西,然后你会看到它是否在没有 if 循环的情况下更新。那么如果是这样,你的 if 循环应该可以工作。

回答by David

You work this?

你干这个?

$timestamp=mktime();
$updateQuery = "UPDATE myTable SET foo=1, timestamp={$timestamp} WHERE bar=2";
mysql_query($updateQuery);

$updateQuery = "SELECT COUNT(*) FROM myTable WHERE timestamp={$timestamp}";
$res=mysql_query($updateQuery);
$row=mysql_fetch_row($res);
if ($row[0]>0) {
    echo "affected!";
}
else {
    echo "not affected";
}

回答by Innogen

Was My Tought !

是我的主意!

I was just about to tell to check if the function's being called many times !

我正要告诉检查该函数是否被多次调用!

Just a little advice:

只是一点建议:

try using isset() & POST / GET or something like that;

尝试使用 isset() & POST / GET 或类似的东西;

if ( isset( $_POST['Update'] == 'yes' ) ) :

// your code goes here ...

endif;

Hope it was clear and useful, Ciao :)

希望它清楚有用,Ciao :)