I have a simple query:
SELECT u_name AS user_name FROM users WHERE user_name = "john";
I get Unknown Column 'user_name' in where clause
. Can I not refer to 'user_name'
in other parts of the statement even after select 'u_name as user_name'
?
Unknown column in
WHERE
clause caused by lines 1 and 2 and resolved by line 3:$sql = "SELECT * FROM users WHERE username =".$userName;
$sql = "SELECT * FROM users WHERE username =".$userName."";
$sql = "SELECT * FROM users WHERE username ='".$userName."'";
I had the same problem, I found this useful.
remember to put $user in ' ' single quotes.
corrected:
While you can alias your tables within your query (i.e., "SELECT u.username FROM users u;"), you have to use the actual names of the columns you're referencing. AS only impacts how the fields are returned.
See the following MySQL manual page: http://dev.mysql.com/doc/refman/5.0/en/select.html
(...)
SQL is evaluated backwards, from right to left. So the where clause is parsed and evaluate prior to the select clause. Because of this the aliasing of u_name to user_name has not yet occurred.