Row Count Help in PDO

Hey there,

I am trying to count the number of rows returned by a PDO. I have tried to use http://php.net/manual/en/pdostatement.rowcount.php (Example #2) to try and make my select query.

here’s the code



$query['1'] = 'SELECT COUNT(*) FROM users WHERE `email` = :email AND `password` = :password';

$x = $pdo->prepare($query['1']);
$values['1'] = array('email' => $email, 'password' => $password);

if($res = $x->execute($values['1'])){
		if($res->fetchColumn() == 1){
				$query['2'] = 'SELECT * FROM users WHERE email= :email AND password= :password';
				$y = $pdo->prepare($query['2']);
				$values['2'] = array('email' => $email, 'password' => $password);
				
				session_start();
				foreach($y->execute($values['2']) as $row)
					{
						$_SESSION['id'] = $row['id'];
						$_SESSION['email'] = $row['email'];
						$_SESSION['name'] = $row['nameFirst'];
					}
			echo "<div class=\\"sucess\\">\
<p>You have been logged in. This page well redirect you to our home page in 5 seconds or less</p>/n</div>";
				}
			}
			else{
				echo "<p class=\\"loginError\\">Your email or Password was wrong, please try again!</p>";
				require('../../views/forms/login.php');
			}


And i am getting an error on line 18 : Call to a member function fetchColumn() on a non-object in /var/www/ageasy/lib/account/login.php on line 18.

Any help would be appreciated.

From the example given by php.net (the link you provided), you should use the same reference variable as execute() so it should be this:

$x->fetchColumn()

Why the need to count the rows, check the count of rows is 1 (what if there is 2?) if so, then go back to the db and get the details.

When in fact all you need to do is learn how to handle:

If user/pass exists in the db then get their details otherwise fail.

Is there some other reason why you need to count the rows prior to querying the db?