Why won't my mysql_query PHP command work!

Discussion in 'PHP' started by Imozeb, Feb 8, 2010.

  1. #1
    I am trying to check whether or not a new user's username exists in my database. So I typed the code that follows, but for some reason the mysql_query command outputs something like "resource ID # 3" and mysql_num_rows won't accept it. How do I fix this?

    My database setup looks like this "USERNAMEDB" --> "USERNAMES" --> "NAME"


    //check used username ($varname == name from field)
    $loginnametest = $varname;

    mysql_select_db("USERNAMEDB");
    $loginresult = mysql_query("SELECT * FROM USERNAMES WHERE NAME = '$loginnametest';");
    $numrows = mysql_num_rows($loginresult);
    //failed username query
    if($numrows >= 1)
    {
    $usernametaken = "Sorry but the username is already taken.";
    }
    else
    {
    //continue
    }

    Thanks in advance!

    If you guys need anymore info please tell me.

    ~Imozeb :)
     
    Imozeb, Feb 8, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Make sure:

    1. You've connected to your database sucessfully.

    2. $varname exists.

    3. Sanitize all data (namely user submitted), when parsing to db.

    4. You have a semi-colon at the end of your mysql_query, remove it.

    Use the following code and report back if any errors appear:

    <?php
    error_reporting(E_ALL);
    
      $loginnametest = mysql_real_escape_string($varname);
      $loginresult = mysql_query("SELECT * FROM USERNAMES WHERE NAME = $loginnametest") or die(mysql_error());
      $numrows = mysql_num_rows($loginresult) or die(mysql_error());
      //failed username query..
      if ($numrows >= 1) {
          echo "Sorry but the username is already taken.";
      } else {
      //passed check..
          echo "Username does'nt exist";
      }
    ?>
    PHP:
     
    danx10, Feb 8, 2010 IP
  3. zytex

    zytex Well-Known Member

    Messages:
    599
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    110
    #3
    From a quick look i noticed you have a ;

    $loginresult = mysql_query("SELECT * FROM USERNAMES WHERE NAME = '$loginnametest';");

    Which isn't necessary, try removing it then see if the script works.
     
    zytex, Feb 8, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    1. You've connected to your database sucessfully. (Not sure)

    2. $varname exists. (Success)

    3. Sanitize all data (namely user submitted), when parsing to db. (Success, already had done that to $varname)

    4. You have a semi-colon at the end of your mysql_query, remove it. (OMG how'd that get in there? :) )

    Here are the errors reported:

    Unknown column 'testname' in 'where clause'

    So....... what now? :)
     
    Imozeb, Feb 8, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Are you sure the column exists??....

    Try this: (although I don't think I've changed any functionality).

    <?php
    error_reporting(E_ALL);
    
      $loginnametest = mysql_real_escape_string($varname);
      $loginresult = mysql_query("SELECT * FROM `USERNAMES` WHERE `NAME` = $loginnametest") or die(mysql_error());
      $numrows = mysql_num_rows($loginresult) or die(mysql_error());
      //failed username query..
      if ($numrows >= 1) {
          echo "Sorry but the username is already taken.";
      } else {
      //passed check..
          echo "Username does'nt exist";
      }
    ?>
    PHP:
     
    danx10, Feb 8, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am sure the column exists. How can I be sure I connect to the database.
     
    Imozeb, Feb 8, 2010 IP
  7. jNorth

    jNorth Peon

    Messages:
    202
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Change this line

    $loginresult = mysql_query("SELECT * FROM `USERNAMES` WHERE `NAME` = $loginnametest")

    to

    $loginresult = mysql_query("SELECT * FROM `USERNAMES` WHERE `NAME` = '".$loginnametest."'")
     
    jNorth, Feb 12, 2010 IP