MySQL: Unknown column in where clause error

user169551 picture user169551 · Sep 26, 2009 · Viewed 61.8k times · Source

I have a PHP script and for some reason mysql keeps treating the value to select/insert as a column. Here is an example of my sql query:

$query = mysql_query("SELECT * FROM tutorial.users WHERE (uname=`".mysql_real_escape_string($username)."`)") or die(mysql_error());

That turns into:

SELECT * FROM tutorial.users WHERE (uname=`test`)

The error was:

Unknown column 'test' in 'where clause'

I have also tried:

SELECT * FROM tutorial.users WHERE uname=`test`

Answer

tpdi picture tpdi · Sep 26, 2009

In MySql, backticks indicate that an indentifier is a column name. (Other RDBMS use brackets or double quotes for this).

So your query was, "give me all rows where the value in the column named 'uname' is equal to the value in the column named 'test'". But since there is no column named test in your table, you get the error you saw.

Replace the backticks with single quotes.