mysql_affected_rows返回0

时间:2012-05-15 10:55:38

标签: php mysql

我在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();
}

5 个答案:

答案 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);

http://php.net/manual/en/function.mysql-affected-rows.php

答案 4 :(得分:0)

由于每个人似乎都认为您使用PDO,而对我来说看起来更像MySQLihere是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();
}