#1
  1. No Profile Picture
    pan
    Guest
    Devshed Newbie (0 - 499 posts)
    Hi, how can I check if a user_id is already being used by another user and automatically tell the user that the id he/she requested is already in use (user_id is PRIMARY KEY and UNIQUE ID in a mysql table)
    Thanks

    ------------------
  2. #2
  3. No Profile Picture
    rod k
    Guest
    Devshed Newbie (0 - 499 posts)
    $result=mysql_query("select * from usertable where userid='$userid'");
    $exists=mysql_num_rows($result);
    if ($exists)
    {
    //username already used
    }
    else
    {
    //username not used
    }
  4. #3
  5. No Profile Picture
    ExPulse
    Guest
    Devshed Newbie (0 - 499 posts)
    Faster way

    $result=mysql_query("select count(*) from usertable where userid='$userid'");
    $number = mysql_fetch_row($result);
    if ($number[0]>='1')
    {
    //username already used
    }
    else
    {
    //username not used
    }
  6. #4
  7. No Profile Picture
    rod k
    Guest
    Devshed Newbie (0 - 499 posts)
    Yes and no.

    Using count(*) would be faster when there is actual data returned i.e. userid is used AND the data returned is large. The fastest way would be to return a single field that contains an integer (the smaller the better) or a char(n) field where n is small (less than 5 characters). This reduces the overhead of returning large data and the overhead of mysql performing the count() function.

    Using a boolean conditional is faster than a comparison.

    The num_rows function is quicker than fetch_row because it returns an integer, not an array (doesn't matter that the array is one element and that element is an integer.)

Similar Threads

  1. dynamically created check boxes
    By wduffee in forum PHP Development
    Replies: 3
    Last Post: February 3rd, 2004, 02:10 PM
  2. Floating elements in Mozilla
    By joh in forum HTML Programming
    Replies: 2
    Last Post: December 28th, 2003, 11:00 PM
  3. Do compilers optimize some / into *?
    By Jason Doucette in forum C Programming
    Replies: 46
    Last Post: December 25th, 2003, 09:47 PM
  4. called too early to check prototype at mediawrap.cgi
    By jenniefer in forum Perl Programming
    Replies: 4
    Last Post: December 20th, 2003, 09:37 AM
  5. Grouping for Last entry made
    By GaryN in forum MySQL Help
    Replies: 0
    Last Post: November 25th, 2003, 09:48 AM

IMN logo majestic logo threadwatch logo seochat tools logo