简体   繁体   English

MySql affected_rows总是为0,但UPDATE有效

[英]MySql affected_rows always 0 but UPDATE works

The following code in question always logs that the affected_rows was 0. I check my DB, the row updates fine everytime. 以下代码总是记录affected_rows为0.我检查我的数据库,每次都更新行。 Why is it 0? 为什么是0?

public static function updateNextRunTime($nextRunTime)
    {
        $mysqli = new mysqli(GDB_HOST, GDB_USERNAME, GDB_PASSWORD, GDB_NAME);

        if ($mysqli->connect_errno)
        {
            throw new Exception('DB Connection Failed. Error Code: ' . $mysqli->connect_errno);
        }

        $cmd = $mysqli->prepare("UPDATE balanceagent SET NextRunTime = ? WHERE Id = 1");
        if (!$cmd)
        {
            throw new Exception($mysqli->error);
        }

        $cmd->bind_param('s', $nextRunTime);
        $cmd->execute();

        $rows = $mysqli->affected_rows;

        $cmd->close();
        $mysqli->close();

        if ($rows != 1)
        {
            logToFile("Balance Agent Error - Failed to update the next run time! - Affected rows: " . $rows);
        }
    }

Your should check $cmd->affected_rows; 你应该检查$cmd->affected_rows; instead of $mysqli->affected_rows; 而不是$mysqli->affected_rows;

For prepared statements you should be using the mysqli_stmt::affected_rows form : 对于准备语句,您应该使用mysqli_stmt :: affected_rows表单:

$cmd->bind_param('s', $nextRunTime);
$cmd->execute();

$rows = $cmd->affected_rows;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM