How to verify two different fields using php and my sql ?

Discussion in 'PHP' started by brajeshnepal, Feb 5, 2007.

  1. #1
    I must admit i am relatively new to php $ mysql. Please some suggest me, i want to check for the duplicate entry on my database for two fields usernae and email. My code is executing perfectely for username but not for email . I am not using any primary or unique key on both of them. Below are teh codes :

    The one that is working for username :
    /**
    * Returns true if the username has been taken
    * by another user, false otherwise.
    */
    function usernameTaken($username){
    global $conn;
    if(!get_magic_quotes_gpc()){
    $username = addslashes($username);
    }
    $q = "select username from users where username = '$username'";
    $result = mysql_query($q,$conn);
    return (mysql_numrows($result) > 0);
    }

    And the below for Email check doenn't work :

    /**
    * Returns true if the E-Mail has been taken
    * by another user, false otherwise.
    */

    function emailTaken($email){
    global $conn;
    if(!get_magic_quotes_gpc()){
    $email = addslashes($email);
    }
    $q = "select E-Mail from users where E-Mail = '$email'";
    $result = mysql_query($q,$conn);
    return (mysql_numrows($result) > 0);
    }

    Warning that is flashed out for email is :
    Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in c:\wamp\www\user\register.php on line 31

    Please some one suggest me with the code or modifications.

    Thanks is advance.
     
    brajeshnepal, Feb 5, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Try adding backquotes around the field name:
    "SELECT `E-Mail` FROM users WHERE `E-Mail` = '$email'";
     
    phper, Feb 5, 2007 IP