简体   繁体   中英

False positive PDO rowcount

I'm trying to test if a user has activated their account using rowcount . Currently it's returning a positive even when I deliberately edit the database data to fail.

I tested my SQL to see if that was the problem, it seemed to return the expected values. I'm assuming it is something to do with my PHP code

try
{
  $db = new Database;
  $query = "SELECT COUNT(*),accountStatus, email FROM users WHERE email  = :email AND accountStatus ='Active'";   
  $stmt = $db->prepare($query);    
  $stmt->bindValue(':email', $email);  
  $stmt->execute();   
  $count = $stmt->rowCount();  
}
catch(Exception $e)
{
 $errors[] = ["name" => "email", "error" => "Something went wrong contact the administrator or try again later"];
} 
if(count($count > 0))
{
  return true;
}       
else
{
  return false;
}

From the docs :

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM