php MySQL:where 子句错误中的未知列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1480327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: Unknown column in where clause error
提问by user169551
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:
我有一个 PHP 脚本,出于某种原因,mysql 一直将要选择/插入的值视为一列。这是我的 sql 查询的示例:
$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'
“where 子句”中的未知列“test”
I have also tried:
我也试过:
SELECT * FROM tutorial.users WHERE uname=`test`
回答by tpdi
In MySql, backticks indicate that an indentifier is a column name. (Other RDBMS use brackets or double quotes for this).
在 MySql 中,反引号表示一个标识符是一个列名。(其他 RDBMS 为此使用方括号或双引号)。
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.
所以你的查询是,“给我所有的行,其中名为 'uname' 的列中的值等于名为 'test' 的列中的值”。但是由于您的表中没有名为 test 的列,您会看到您看到的错误。
Replace the backticks with single quotes.
用单引号替换反引号。
回答by meder omuraliev
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
奇怪的?为何如此?它确切地说出了什么问题。您的表中没有“测试”列。你确定你有合适的桌子吗?'tutorial.users' ? 您确定该表没有以不同的方式命名吗?也许你打算做
SELECT * from users WHERE uname = 'test';
You have to reference only the table name, not the database.. assuming the database is named tutorial
您必须仅引用表名,而不是数据库.. 假设数据库已命名 tutorial
回答by Thierry Vincent
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-
注意 $uname 周围的单引号。当您回显查询时,这是输出 -
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-
但是,如果您错过了查询中 $uname 变量周围的引号,这就是您将得到的-
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.
在 MySQL 服务器上,这 2 个查询是不同的。thierry 是输入字符串并正确封装在引号中,而在第二个查询中,它不是,这会导致 MySQL 出错。
I hope this helps and excuse my englis which is not very good
我希望这有助于并原谅我的英语不是很好
回答by SirBT
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.
我检查了表格中的那一列,结果是,我在表格中将其拼写为“depature”而不是“departure”,因此抛出了错误消息。
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.
我希望这有帮助。
回答by Himanshu Chauhan
I also faced the issue of "Unknown column in where clause" when executing the following from linux (bash) command line.
从 linux (bash) 命令行执行以下命令时,我还遇到了“where 子句中的未知列”的问题。
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.
我不得不用双引号 "test01" 替换单引号 'test01' 。它对我有用。执行 sql 查询的方式和方式有所不同。
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.
当在脚本中为变量赋值时,稍后在必须由脚本执行的 sql 语句中使用该变量时,略有不同。
If suppose variable is
如果假设变量是
var=testing
and you want to pass this value from within script to mysql, then single quotes work.
并且您想将此值从脚本内传递给 mysql,然后单引号起作用。
select '$var'
So different engines might evaluate backticks and quotes differently.
因此,不同的引擎可能会以不同的方式评估反引号和引号。
This is my query that worked from linux command line.
这是我从 linux 命令行工作的查询。
mysql -u support -pabc123 -e 'select * from test.sku where dispsku="test01" ; '

