php mysqli affected_rows

时间:2011-03-25 20:58:25

标签: php mysqli rows-affected

我只是在做一些测试,我无法弄清楚我是否正确地做这件事。查询将更新行..但affected_rows总是返回0 ..为什么?

<?php 

$connection = new mysqli('localhost', 'user', 'pass', 'db');
if (mysqli_connect_errno()) {
   printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
   exit;
}

$email      = $connection->real_escape_string($_GET['email']);
$activation = $connection->real_escape_string($_GET['hash']);

//$query =  $connection->query("SELECT email, activationCode, active FROM users WHERE email='".$email."' AND hash='".$activation."' AND active='0'");
$select = $connection->query("UPDATE users SET active = '1' WHERE email='".$email."' AND activationCode='".$activation."' AND active='0'");


printf("Affected rows (UPDATE): %d\n", $select->affected_rows);



$connection->close();
?>

1 个答案:

答案 0 :(得分:6)

它表示$select->affected_rows而不是$connection->affected_rows

$select->affected_rows包含有关上次查询影响的行数(可能成功与否)的信息,而connection保存包含查询结果数据的数据库管理器对象。