Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Check for Unique Username

    The title say it all. How can I create a script that will check and see in my database to see if there is already a unique Username. I'm doing this for a Register page.

    Thanks for you help in advance.

  • #2
    Regular Coder
    Join Date
    Aug 2006
    Posts
    311
    Thanks
    0
    Thanked 1 Time in 1 Post
    Depending on how your database is setup you would want to create a form and the mysql query would look something like this

    PHP Code:
     $sql=mysql_query("SELECT user_name FROM your_table_here WHERE username='{$_POST['userid']}'");
    $num_rows=mysql_num_rows($sql);

    if(
    $num_rows >1){

    echo 
    'User Id already exists';

    here is a tutorial about forms. http://www.tizag.com/phpT/forms.php

    It should help you out with the basics of this.

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You'll not want to check for $num_rows > 1... this will only return TRUE if the user name exists twice or more in the database. That code will allow a name to be registered twice before disallowing it to be registered again.

    All you really need to check is if any rows were returned, since 0 is equivalent to FALSE and everything above equates to TRUE:
    PHP Code:
    $qry sprintf'SELECT `id` FROM `users` WHERE `userName` = \'%s\''mysql_real_escape_string$_POST['userName'] ) );
    $res mysql_query$sql );
    $num mysql_num_rows$res );

    if ( 
    $num )
    {
        echo 
    'This user already exists';

    Last edited by kbluhm; 01-07-2008 at 01:22 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •