MySql affected_rows总是为0,但UPDATE有效

时间:2013-08-02 00:40:25

标签: php mysql

以下代码总是记录affected_rows为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);
        }
    }

2 个答案:

答案 0 :(得分:1)

你应该检查一下 $cmd->affected_rows; 代替 $mysqli->affected_rows;

答案 1 :(得分:1)

对于准备好的陈述,您应该使用mysqli_stmt::affected_rows表格:

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

$rows = $cmd->affected_rows;