...

View Full Version : Unknown column 'Dubz' in 'where clause'



Dubz
12-18-2011, 10:07 PM
My query:


$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:
http://img838.imageshack.us/img838/381/7b1a5798ee4c4866a661080.png

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.

Old Pedant
12-19-2011, 03:38 AM
$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.

Old Pedant
12-19-2011, 03:41 AM
By the way, the right way to see the structure of a table in MySQL is either

DESCRIBE tablename;

or

SHOW CREATE TABLE tablename;

(The two commands are *NOT* the same thing. Try them both to see the difference.)

Dubz
12-20-2011, 11:36 PM
$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 :p
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum