Search code examples
phpmysqlirows-affected

php mysqli affected_rows


I'm just doing some testing with this, and I cant figure out if I am doing this the correct way. The query will update the row.. But the affected_rows always returns 0.. Why?

<?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();
?>

Solution

  • It says $select->affected_rows rather than $connection->affected_rows.

    $select->affected_rows contains the information about how many rows were affected by the last query (that could be successful or not) while connection holds the database manager object which contains the data regarding query results.