简体   繁体   中英

How to optimize mysql query registration

At the time of registration, I am checking for there field username , email , phonenumber

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.

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'

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