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

APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Don't know if it helps but Example #1 on this page http://php.net/manual/en/pdostatement.rowcount.php works when I converted it to a SELECT statement instead of a DELETE.  Maybe you need to move rowCount() to the line after execute().
Chris StanyonWebDevCommented:
As Dave has said, you need rowCount() AFTER you've executed the query.

Couple of other points.

$rows will only ever contain the first record from the query, even if there are more than 1. If you want to access all the records, then fetch() should be used in a loop.

If you only want the count, rather than the data, a better way to do it would be to select a COUNT:

SELECT COUNT(first_name) as count FROM users WHERE email_address = :email;

The reason why your second example returns an odd number is because you're not counting the number of rows - you're counting the number of keys in an array. $rows will be an array containing a single record from your DB. Each index in the array will be a column name, so because you're selecting first_name and archived, then a count on $rows will return 2 (2 fields).

If you want to take that route, then you would call fetchAll:

$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
echo 'count= ' . count($rows);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.