简体   繁体   English

PHP + MySql,或带有多个错误检查的查询

[英]PHP + MySql, OR query with multiple error checks

Currently I have to use 2 queries to check if database allready contains email or ip. 目前,我必须使用2个查询来检查数据库allready是否包含电子邮件或ip。

My code now: 我的代码现在:

        $link = $db->prepare("SELECT * FROM users WHERE email=?");
        if($link->execute(array($login))){
            if($link->rowCount() > 0){
                $errmsg_arr[] = 'Email is already in use';
                $errflag = true;
            }
        }

        $link = $db->prepare("SELECT * FROM users WHERE Ip=?");
        if($link->execute(array($ip))){
            if($link->rowCount() > 0){
                $errmsg_arr[] = 'There is allready account registered with this IP address';
                $errflag = true;
            }
        }

Is there way to check if email or IP is allready used if I use this query and how to do it? 如果我使用此查询,是否可以检查是否已使用电子邮件或IP,以及如何执行?

$link = $db->prepare("SELECT * FROM users WHERE email=? OR Ip=?");

EDIT: To be clear, I want to give 2 different errors, not one that tells user "Email or IP is allready in use" 编辑:要明确,我想给出2个不同的错误,而不是告诉用户“电子邮件或IP已准备就绪”

    if($link->execute(array($login, $ip))){
        // WHAT HERE?
        // CHECK IF EMAIL IS ON USE
        $errmsg_arr[] = 'Email is already in use';

        //CHECK IF IP IS USED
        $errmsg_arr[] = 'There is allready account registered with this IP address';

        $errflag = true;
    }

So, is this possible with just 1 query? 那么,仅用1个查询就能做到吗?

I want to minimize use of queries. 我想最小化查询的使用。

Thanks 谢谢

Try this: (and since you use prepare() and execute, read here ): 尝试以下操作:(由于您使用prepare()并执行,请在此处阅读):

$link = $db->prepare("SELECT * FROM users WHERE email=? OR Ip=?");
if($link->execute(array($email, $ip))){
        if($link->rowCount() > 0){
            $errmsg_arr[] = 'Email or IP is already in use';
            $errflag = true;
        }
    }

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

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