简体   繁体   English

如何优化mysql查询注册

[英]How to optimize mysql query registration

At the time of registration, I am checking for there field username , email , phonenumber 在注册时,我正在检查字段usernameemailphonenumber

With three query. 具有三个查询。 snippet is following: 片段如下:

$query = "SELECT *
              FROM 
                users 
              WHERE  username='$userName'";
    $result = mysql_query($query, $this->con);
    $count_username = mysql_num_rows($result);
  if($count_username <= 0){
  $query = "";$result = "";
    $query = "SELECT *
              FROM 
                users 
              WHERE email='$email' ";
    $result = mysql_query($query, $this->con);
    $count_email = mysql_num_rows($result);
  }
  if($count_username <= 0 && $count_email <= 0){
  $query = "";$result = "";
    $query = "SELECT *
              FROM 
                users 
              WHERE  phone_number='$phone'";
    $result = mysql_query($query, $this->con);
    $count_phone = mysql_num_rows($result);
  }

Is there is way to do this with single query, or is there is other way to optimize this code??? 有没有一种方法可以通过单个查询做到这一点,或者有其他方法可以优化此代码? Sorry for using mysql extension. 抱歉使用mysql扩展。

If i use single query: 如果我使用单个查询:

$query = "SELECT *  FROM  users WHERE  username='$userName' && email='$email' && phone_number='$phone'";

I am unable to show different error: 我无法显示其他错误:

  • username exists 用户名存在
  • email exists 电子邮件存在
  • phone exists 手机存在

I don't want to show error like: 我不想显示错误:

username/phone/exists exists

You can have something like this: 你可以有这样的事情:

SELECT  CASE WHEN username = '$userName' THEN 'username exists'
            WHEN email = '$email' THEN 'email exists'
            ELSE 'phone exists'
        END Result
FROM    users
WHERE   username = '$userName'
        OR email = '$email'
        OR phone_number = '$phone'

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

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