1

我对mysql_affected_rows()PHP 中的函数有疑问。我使用 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();
}
4

5 回答 5

4

It seems you're using PDO, not the mysql_* functions. Therefore, you should uso PDOs rowCount function:

$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();
}
于 2012-05-15T10:57:49.873 回答
1

使用语句时,使用affected_rows获取受影响的行数:UPDATE

$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()语句之前

于 2012-05-15T11:02:00.103 回答
0

您需要将连接作为参数传递给函数。

echo mysql_affected_rows($this->conn);

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

于 2012-05-15T10:59:43.517 回答
0

使用这个http://www.php.net/manual/en/pdostatement.rowcount.php

PDOStatement::rowCount 返回受查询影响的行数

于 2012-05-15T10:58:41.677 回答
0

由于每个人似乎都认为您使用PDO,而在我看来它更像MySQLi是 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();
}
于 2012-05-15T11:07:15.423 回答