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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts

    Exclamation Unknown column 'Dubz' in 'where clause'

    My query:
    PHP Code:
    $query mysql_query("SELECT id FROM users WHERE username=$username AND password=$password") or die (mysql_error());
    $num mysql_num_rows($query);
    if(
    $num!=1) die("User not found!");
    $account mysql_fetch_array($query); 
    I echoed out $username and $password and they match the values in the db but I keep getting this error: Unknown column 'Dubz' in 'where clause'. My table column names are correct and it seems like it should work but it won't. Also when I try to load the structure of the users table i get this error:


    I have tried using * instead of id and have tried using the ticks around the names but no luck. I don't know what the problem is but I would like to get this fixed ASAP.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Code:
    $sql = "SELECT id FROM users WHERE username='$username' AND password='$password'";
    
    echo "<hr/>DEBUG SQL: " . $sql . "<hr/>\n";
    
    $query = mysql_query( $sql ) or die (mysql_error());
    (1) Learn to DEBUG DEBUG DEBUG, as shown there. (You remove or comment out the debug after the code starts working.) If you had done that with your original query, I *think* you would have figured it out immediately.

    (2) You were missing the apostrophes that are now in that query, above.

    HINT: When you don't put apostrophes around a value, then the value must either be a number or a field name. If it's not a number, MySQL (indeed, all dialects of SQL) assumes it is a field name.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Dubz (12-20-2011)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    By the way, the right way to see the structure of a table in MySQL is either
    Code:
    DESCRIBE tablename;
    
    or 
    
    SHOW CREATE TABLE tablename;
    (The two commands are *NOT* the same thing. Try them both to see the difference.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    $sql = "SELECT id FROM users WHERE username='$username' AND password='$password'";
    
    echo "<hr/>DEBUG SQL: " . $sql . "<hr/>\n";
    
    $query = mysql_query( $sql ) or die (mysql_error());
    (2) You were missing the apostrophes that are now in that query, above.

    HINT: When you don't put apostrophes around a value, then the value must either be a number or a field name. If it's not a number, MySQL (indeed, all dialects of SQL) assumes it is a field name.
    Got it to work now. Thanks
    Their was also some problem with my db and I couldn't figure it out but it stopped (must have been the webhost). Anyways thanks for the help.


  •  

    Posting Permissions

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