MySQL:where 子句中的未知列错误
我有一个 PHP 脚本,由于某种原因 mysql 一直将要选择/插入的值视为列。这是我的 sql 查询的示例:
$query = mysql_query("SELECT * FROM tutorial.users WHERE (uname=`".mysql_real_escape_string($username)."`)") or die(mysql_error());
变成:
SELECT * FROM tutorial.users WHERE (uname=`test`)
错误是:
“where”中的未知列“test” 条款'
我也尝试过:
SELECT * FROM tutorial.users WHERE uname=`test`
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`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 MySql 中,反引号表示标识符是列名。 (其他 RDBMS 使用括号或双引号)。
所以你的查询是,“给我所有行,其中名为“uname”的列中的值等于名为“test”的列中的值”。但由于表中没有名为 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.
诡异的?为何如此?它确切地说出出了什么问题。您的表中没有“测试”列。您确定您有正确的桌子吗? “教程.用户”?您确定该表的名称没有不同吗?也许您打算这样做
您必须仅引用表名称,而不是数据库..假设数据库名为
tutorial
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
You have to reference only the table name, not the database.. assuming the database is named
tutorial
示例:
注意 $uname 周围的单引号。当您回显查询时,这就是输出 -
但是,如果您在查询中错过了 $uname 变量周围的引号,这就是您将得到的 -
在 MySQL 服务器上,这两个查询是不同的。 thierry 是输入字符串,并正确封装在引号中,而在第二个查询中,它不是,这会导致 MySQL 中出现错误。
我希望这有帮助,请原谅我的英语不太好
example:
Note the single quotes around the $uname. When you echo the query, this is the output-
However if you miss the quote around the $uname variable in your query, this is what you'll get-
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
我也遇到了同样的问题,结果发现是一个错字。
我的错误消息是:
我检查了表中的这一列,结果发现,我在表中将其拼写为“出发”而不是“出发”,因此抛出错误消息。
我随后将查询更改为:
并且它有效!
所以我的建议明确是,仔细检查您是否正确拼写了列名称。
我希望这有帮助。
I had the same issue and it turned out to be a typo.
My error message was:
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:
and it worked!
So my advise clearly is, double check that you spelt the column name properly.
I hope this helped.
从 linux (bash) 命令行执行以下命令时,我还遇到了“where 子句中的未知列”的问题。
这就是我得到的结果,
我必须将单引号 'test01' 替换为双引号 "test01" 。这对我有用。执行 sql 查询的方式和方式有所不同。
当在脚本中为变量赋值时,以及稍后在必须由脚本执行的 sql 语句中使用该变量时,存在细微差别。
如果假设变量是
并且您想将该值从脚本内传递到 mysql,那么单引号就可以了。
因此,不同的引擎可能会以不同的方式评估反引号和引号。
这是我从 linux 命令行运行的查询。
I also faced the issue of "Unknown column in where clause" when executing the following from linux (bash) command line.
This is what I got
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
and you want to pass this value from within script to mysql, then single quotes work.
So different engines might evaluate backticks and quotes differently.
This is my query that worked from linux command line.