简体   繁体   中英

Why does my pdo statement result always return true?

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?

Change this:

$rows = (int) $stmt->fetchColumn();

And it should work.

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