我在PHP中遇到mysql_affected_rows()
函数的问题。我使用MySQL更新,在phpMyAdmin中我可以看到,'确认'从0变为1,但mysql_affected_rows
仍然返回0!我找不到解决方案。我的代码是:
$query = "UPDATE visits
SET confirmed = 1
WHERE id = ? AND confirmed = 0 AND expire > now() - INTERVAL 10 MINUTE;";
$stmt = $this->conn->stmt_init();
if($stmt->prepare($query)) {
$stmt->bind_param('i',$id); //$id is a function parameter
$res = $stmt->execute();
$stmt->close();
echo mysql_affected_rows();
}
答案 0 :(得分:4)
看来你正在使用PDO,而不是mysql_ *函数。 因此,您应该使用PDO rowCount函数:
$query = "UPDATE visits
SET confirmed = 1
WHERE id = ? AND confirmed = 0 AND expire > now() - INTERVAL 10 MINUTE;";
$stmt = $this->conn->stmt_init();
if($stmt->prepare($query)) {
$stmt->bind_param('i',$id); //$id is a function parameter
$res = $stmt->execute();
echo $stmt->rowCount();
$stmt->close();
}
答案 1 :(得分:1)
在使用UPDATE
声明时,使用affected_rows获取受影响的行数:
$stmt = $this->conn->stmt_init();
if($stmt->prepare($query)) {
$stmt->bind_param('i',$id); //$id is a function parameter
$res = $stmt->execute();
echo $stmt->affected_rows;
$stmt->close();
}
它还需要在close()
陈述
答案 2 :(得分:0)
使用此http://www.php.net/manual/en/pdostatement.rowcount.php
PDOStatement :: rowCount返回受查询影响的行数
答案 3 :(得分:0)
您需要将连接作为参数传递给函数。
echo mysql_affected_rows($this->conn);
答案 4 :(得分:0)
由于每个人似乎都认为您使用PDO,而对我来说看起来更像MySQLi,here是MySQLi方式:
$query = "
UPDATE visits
SET confirmed = 1
WHERE id = ?
AND confirmed = 0
AND expire > now() - INTERVAL 10 MINUTE
";
$stmt = $this->conn->stmt_init();
if ($stmt->prepare($query)) {
$stmt->bind_param('i', $id); //$id is a function parameter
$res = $stmt->execute();
echo $stmt->affected_rows; // Here's the good stuff
$stmt->close();
}