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`
解决方案
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.
本文介绍了一种常见的MySQL错误——将值误认为是列名的问题及解决方法。通过一个具体的PHP脚本示例,解释了如何正确地在SQL查询中使用变量,避免将变量值误解为列标识符。

被折叠的 条评论
为什么被折叠?



