Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3439994
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:22:44+00:00 2026-05-18T08:22:44+00:00

How do I check if username or email exists and then put a error

  • 0

How do I check if username or email exists and then put a error message in my error array. Right now i have:

$sql = "SELECT username, email FROM users WHERE username = '" . $username . "' OR email = '" . $email . "'";

$query = mysql_query($sql);

if (mysql_num_rows($query) > 0)
{
 echo "That username or email already exists";
}

But I want to check if it is the username OR the email that is existing and then put:

error[] = “username is existing”; //if the username is existing

error[] = “email is existing”; //if the email is existing

How to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:22:45+00:00Added an answer on May 18, 2026 at 8:22 am

    It would be easier if you just did a quick true/false check in the SQL and checked the flag that came back.

    $sql = "SELECT "
      . "(SELECT 1 FROM `users` WHERE `username` = '" . mysql_real_escape_string($username) . "'), "
      . "(SELECT 1 FROM `users` WHERE `email` = '" . mysql_real_escape_string($email) . "')";
    
    $query = mysql_query($sql);
    
    if (mysql_num_rows($query) > 0) {
      $foundFlags = mysql_fetch_assoc($query);
      if ($foundFlags['username']) {
        $error[] = "username is existing";
      }
    
      if ($foundFlags['email']) {
        $error[] = "email is existing";
      }
    } else {
      // General error as the query should always return
    }
    

    When it does not find an entry, it will return NULL in the flag, which evaluates to false, so the if condition is fine.

    Note that you could generalise it for a field list like this:

    $fieldMatch = array('username' => $username, 'email' => $email);
    
    $sqlParts = array();
    foreach ($fieldMatch as $cFieldName => $cFieldValue) {
      $sqlParts[] = "(SELECT 1 FROM `users` WHERE `" . $cFieldName . "` = '" . mysql_real_escape_string($cFieldValue) . "')";
    }
    
    
    $sql = "SELECT " . implode(", ", $sqlParts);
    
    $query = mysql_query($sql);
    
    if (mysql_num_rows($query) > 0) {
      $foundFlags = mysql_fetch_assoc($query);
      foreach ($foundFlags as $cFieldName => $cFlag) {
        if ($foundFlags[$cFieldName]) {
          $error[] = $cFieldName . " is existing";
        }
      }
    } else {
      // General error as the query should always return
    }
    

    NB. Note that assumes all fields are strings, or other string-escaped types (eg. date/time).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array I want to check whether the username and Email values
i have a signup page and i want to check username exists or not
i have a simple question. how can i check if the username or email
I have a javascript page which checks an email and username, this works fine
I have a CustomValidator that is being used to check username availability, but it
Whats the best way to check if a username exists in a MySQL table?
I want have an insert query, but before inserting I check whether the username
ive created a form and im using php to check if the username exists
I'm trying to check if an email exists in the system, before a user
I have a function that checks if the email address already exists in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.