简体   繁体   English

为什么我的pdo语句结果总是返回true?

[英]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 我正在将一个旧的登录脚本从mysql_query转换为pdo,但我有些挣扎

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? 不管表中是否存在电子邮件,代码始终返回true?

Is there not a simple way to do this like count_rows in sql? 有没有像sql中的count_rows这样简单的方法?

Change this: 更改此:

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

And it should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM