views:

378

answers:

2

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`
+4  A: 

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

meder
works now, thanks
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';
halocursed
np. don't forget to select an answer :) and welcome to Stackoverflow.
meder
+10  A: 

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.

tpdi
oh, so that's the real explanation ;)
meder