Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Unknown column in where clause error

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`
like image 637
user169551 Avatar asked Sep 26 '09 03:09

user169551


People also ask

How do you fix Unknown column in field list?

To fix the error above, simply add a quotation mark around the value. You can use both single quotes or double quotes as shown below: INSERT INTO users(username, display_name) VALUES ("jackolantern", 'Jack'); Now the INSERT statement should run without any error.

What is error code 1054 in MySQL?

MySQL error code 1054 occurs if we forget to add single quotes while inserting a varchar value or due to any missing column.

What happen if where clause is not given in query?

If the given condition does not match any record in the table, then the query would not return any row.

What does Unknown column mean in SQL?

The MySQL unknown column in field list error happens when you put a column name in your SQL script that can't be found by MySQL.


1 Answers

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.

like image 74
tpdi Avatar answered Oct 25 '22 13:10

tpdi