Jump to content

Username Check


master82

Recommended Posts

I've created a registration form for my site and started the process of validating the input (check for blanks, length, passwords match etc) but I've come across a problem when it comes to checking the database to see if the requested username is already in use.

I've already included the connection info, from what I remember I have to do an IF(... SELECT and LIMIT or something like that.

Could anyone show me how to do this?
Link to comment
Share on other sites

One way to check for a username already existing would be:
SELECT user_id FROM user_table WHERE username = '$username'
Make sure to replace the values in your table with mine, anyway if a user_id is returned then you know the username is taken, else you're safe to continue.
Link to comment
Share on other sites

Thanx but I need a way of automatically checking if it is present, hence why I mentioned the IF before (If its already taken then i'll die("username already taken") but if no match is found then the script will continue to add the new user to the database
Link to comment
Share on other sites

You'll want to use Buyocats query and the code will be like this:
[code]//connect to database

$sql = "SELECT user_id FROM user_table WHERE username = '$username' LIMIT 1";
$result = mysql_query($sql);

if(myql_num_rows($result) == 1)
{
    echo "Username in use";
}
else
{
    //continue to register user
}[/code]

Also you might want to set the username field to be UNIQUE too.
Link to comment
Share on other sites

Thanks, thats just what I needed...

if(myql_num_rows($result) == 1)

sorry for the bad explination -> that is what I was after [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.