Link to home
Start Free TrialLog in
Avatar of APD Toronto
APD TorontoFlag for Canada

asked on

PDO Row count

Hi Experts,

I am trying to get the row count of the query, but the following always return 0
$email = $_POST['txtEmail'];
        $result = '';
        
        $query  = "SELECT first_name, archived  FROM users ";
        $query .= "WHERE email_address = :email";
        
        $db = Database::getDB();
        
        $statement = $db->prepare($query);
            $statement->bindValue(':email', $email);
        $row_count = $statement->rowCount();
        $statement->execute();
        $rows = $statement->fetch(PDO::FETCH_ASSOC);
        $statement->closeCursor();
                
        print_variable($rows, 'resv');
        echo 'count= ' . $row_count;

Open in new window


The output is
========START=========
resv
Array
(
    [first_name] => Aleks
    [archived] => 0
)

=========END=========
count= 0

Open in new window


I change it to count it always return 1 or greater, even with an invalid email.
    
        $email = $_POST['txtEmail'];
        $result = '';
        
        $query  = "SELECT first_name, archived  FROM users ";
        $query .= "WHERE email_address = :email";
        
        $db = Database::getDB();
        
        $statement = $db->prepare($query);
            $statement->bindValue(':email', $email);
        $row_count = $statement->rowCount();
        $statement->execute();
        $rows = $statement->fetch(PDO::FETCH_ASSOC);
        $statement->closeCursor();
                
        print_variable($rows, 'resv');
      //  echo 'count= ' . $row_count;
          echo 'count= ' . count($rows);
              

Open in new window


My output
=========START=========
resv

=========END=========
count= 1

Open in new window

SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial