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 360287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:28:29+00:00 2026-05-12T12:28:29+00:00

I want to check for the username in the users table. If it’s found,

  • 0

I want to check for the username in the users table.
If it’s found, then I want to get the id.
Otherwise, I want to insert the username as a new record, then get the id.

Here’s my code:

<?PHP
$sql = "SELECT id FROM users where username = '$username'";
$query = mysql_query($sql) or die(mysql_error());
$result = mysql_num_rows($query);
if($result){
    $row = mysql_fetch_array($query);
    $userid = $row["id"];
}else{
    $sql = "insert into users set username = '$username'";
    $query = mysql_query($sql) or die(mysql_error());
    $userid = mysql_insert_id();
}
?>

How can I optimize or combine these MySQL queries?

Can I select and insert in the same query?

and would it be better and faster?

  • 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-12T12:28:29+00:00Added an answer on May 12, 2026 at 12:28 pm

    It’s better to INSERT first, with the assumption that the username does not exist. If any error is caused by a duplicate conflict, SELECT the existing row.

    <?php
    $sql = "INSERT INTO users SET username = '" 
      . mysql_real_escape_string($username) . "'";
    $query = mysql_query($sql);
    if ($query === true) {
        $userid = mysql_insert_id();
    } else {
        if (mysql_errno() != 1022) { // this is the duplicate key error code
            die(mysql_error());
        }
        $sql = "SELECT id FROM users where username = '"
          . mysql_real_escape_string($username) . "'";
        $query = mysql_query($sql) or die(mysql_error());
        $row = mysql_fetch_array($query);
        if ($row !== false) {
            $userid = $row["id"];
        }
    }
    ?>
    

    The reason this is better is that if you select then insert, someone else might insert the username in the moment between your two SQL statements.

    Of course you should have a UNIQUE constraint on users.username.


    Re your comment: yes, it should be faster that your script. When there is no duplicate username (which is probably more common), you don’t have to run the SELECT.

    The best way to optimize an SQL query is not to run it at all.

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

Sidebar

Related Questions

When I insert a new record in the users table, first check email and
TL;DR I want to check my Coupon table when a new user registers to
i have a signup page and i want to check username exists or not
In my login activity...I am asking the username and password.. I want to check
I want to check if my many column in datagridview is empty. here my
I want to check a Column value when update.If its match insert into another
All, I'm trying to write a function that would check in my users table
I want my 'users' table in my database to contain users of all different
I have written a code that searches specific string from database table.what i want
Hy, I have a users table in mysql with username and alias set to

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.