MySQL: Unknown column in where clause error
Asked Answered
F

5

7

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`
Flub answered 26/9, 2009 at 3:10 Comment(0)
L
39

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.

Lorenalorene answered 26/9, 2009 at 3:43 Comment(0)
S
9

Weird? How so? It says exactly what's wrong. There is no 'test' column in your table. Are you sure you have the right table? 'tutorial.users' ? Are you sure the table isn't named differently? Maybe you meant to do

SELECT * from users WHERE uname = 'test';

You have to reference only the table name, not the database.. assuming the database is named tutorial

Saltcellar answered 26/9, 2009 at 3:13 Comment(2)
Yeah I think he is adding the name of the database in the query.. or maybe he wants to select tutorial.users from users where uname='test';Guyer
np. don't forget to select an answer :) and welcome to Stackoverflow.Saltcellar
B
4

example:

$uname = $_POST['username'];
$sql="SELECT * FROM Administrators WHERE Username LIKE '$uname'"

Note the single quotes around the $uname. When you echo the query, this is the output-

SELECT * FROM Administrators WHERE Username LIKE 'thierry'

However if you miss the quote around the $uname variable in your query, this is what you'll get-

SELECT * FROM Administrators WHERE Username LIKE thierry

On MySQL server, the 2 queries are different. thierry is the input string and correctly encapsulated in quote marks, where as in the second query, it isn't, which causes an error in MySQL.

I hope this helps and excuse my englis which is not very good

Bags answered 10/6, 2012 at 15:52 Comment(1)
Great answer. Helped me better understand when to use ' and when not. ThanksFrenulum
W
0

I had the same issue and it turned out to be a typo. My error message was:

Unknown column 'departure' in 'where clause'

I checked that very column in my table and it turns out that, I had spelt it as "depature" and NOT "departure" in the table, therefore throwing the error message.

I subsequently changed my query to:

Unknown column 'depature' in 'where clause' 

and it worked!

So my advise clearly is, double check that you spelt the column name properly.

I hope this helped.

Wonderland answered 26/9, 2009 at 3:11 Comment(0)
W
0

I also faced the issue of "Unknown column in where clause" when executing the following from linux (bash) command line.

mysql -u support -pabc123 -e 'select * from test.sku where dispsku='test01' ; '

This is what I got

ERROR 1054 (42S22) at line 1: Unknown column 'test01' in 'where clause'

I had to replace the single quotes 'test01' with double quotes "test01" . It worked for me. There's a difference how and the way you're executing sql queries.

When assigning a value to a variable in a script and later, using that variable in a sql statement that has to be executed by the script, there's slight difference.

If suppose variable is

var=testing

and you want to pass this value from within script to mysql, then single quotes work.

select '$var'

So different engines might evaluate backticks and quotes differently.

This is my query that worked from linux command line.

mysql -u support -pabc123 -e 'select * from test.sku where dispsku="test01" ; '
Whatsoever answered 8/4, 2017 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.