0

以下有问题的代码始终记录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);
        }
    }
4

2 回答 2

1

你应该检查 $cmd->affected_rows; 而不是 $mysqli->affected_rows;

于 2013-08-02T00:44:17.277 回答
1

对于准备好的语句,您应该使用mysqli_stmt::affected_rows形式:

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

$rows = $cmd->affected_rows;
于 2013-08-02T00:47:41.723 回答