How to check for an existing value in mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    How to check for an existing value in mysql

    As per usual i'm a newbie.
    What i'm struggling with is a sign up script. I need to check whether a username already exists in my MySQL table. I have searched the forums for answers but nothings been explained or answered.

    Thanks for the help :)
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Markus.

    Simply SELECT from your database using the information provided and then check to see if your query returned anything:

    [code=php]
    $_sql = "
    SELECT
    `id`
    FROM
    `users`
    WHERE
    `id` = '{$id}'
    LIMIT 1";
    $_res = mysql_query($_s ql);

    if( mysql_num_rows( $_res) > 0 )
    {
    // id is taken.
    }
    mysql_free_resu lt($_res);
    [/code]

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Moved from Php Articles Section to the Php forum.

      Comment

      Working...