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
    Mar 2005
    Location
    United States
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check if user exists

    Hey guys I want to write a php code that will check if a user exists in the database. Can someone tell me if there is an easier way to write this code?

    PHP Code:
    $result mysql_query("SELECT * FROM mpx_messages WHERE username='username'") or die(mysql_error()); 
    $row mysql_fetch_array$result );
    $mp_username $row['username'];

    if(
    $mp_username == ""){
    //user doesnt exist
    }else{
    //user exists

    Im not sure but I think there should be an easier way to see if a user exists in a database.

  • #2
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    842
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Here is what I use for one of my sites

    However you get the username to the query is up to you.
    PHP Code:
          $check = mysql_num_rows(mysql_query("SELECT username FROM members WHERE username='$user'")); 

        if($check=="1")
        { ?>
        <br>
        <font size="2" face="Arial, Helvetica, sans-serif">Sorry, the username <i><b><?php echo "$user"?></b></i> 
        is already taken.<br>
        Please choose another one.</font><br>
        <?php }
        else
        { 
    ?>
        <br>
        <font size="2" face="Arial, Helvetica, sans-serif">The username <i><b><?php echo "$user"?></b></i> 
        is available!<br>
        You are good to go.</font><br>
        <?php    }
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    United States
    Posts
    271
    Thanks
    0
    Thanked 0 Times in 0 Posts
    AHA! I knew it, thanks


  •  

    Posting Permissions

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