Reputation: 1430
I am converting an old login script from mysql_query into pdo and I am struggling somewhat
All I am trying to do is find out of an email exist in the database or not!
My Code
public function doesEmailExist($userEmail)
{
$db = new PDO('mysql:host=localhost;dbname=servershop', 'user', 'pass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("SELECT COUNT(`uid`) FROM `user` WHERE `email`= ?");
$stmt->bindValue(1, $userEmail);
try
{
$stmt->execute();
$rows = $stmt->fetchColumn();
if($rows == 1)
{
return true;
}
else
{
return false;
}
}
catch (PDOException $e)
{
die($e->getMessage());
}
}
Regardless of if the email exists in the table or not, the code is always returning true?
Is there not a simple way to do this like count_rows in sql?
Upvotes: 0
Views: 366
Reputation: 7900
Change this:
$rows = (int) $stmt->fetchColumn();
And it should work.
Upvotes: 1