MySQL:where 子句中的未知列错误

发布于 2024-08-06 01:45:39 字数 472 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尘世孤行 2024-08-13 01:45:39

在 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.

泡沫很甜 2024-08-13 01:45:39

诡异的?为何如此?它确切地说出出了什么问题。您的表中没有“测试”列。您确定您有正确的桌子吗? “教程.用户”?您确定该表的名称没有不同吗?也许您打算这样做

SELECT * from users WHERE uname = 'test';

您必须仅引用表名称,而不是数据库..假设数据库名为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

SELECT * from users WHERE uname = 'test';

You have to reference only the table name, not the database.. assuming the database is named tutorial

成熟的代价 2024-08-13 01:45:39

示例:

$uname = $_POST['username'];
$sql="SELECT * FROM Administrators WHERE Username LIKE '$uname'"

注意 $uname 周围的单引号。当您回显查询时,这就是输出 -

SELECT * FROM Administrators WHERE Username LIKE 'thierry'

但是,如果您在查询中错过了 $uname 变量周围的引号,这就是您将得到的 -

SELECT * FROM Administrators WHERE Username LIKE thierry

在 MySQL 服务器上,这两个查询是不同的。 thierry 是输入字符串,并正确封装在引号中,而在第二个查询中,它不是,这会导致 MySQL 中出现错误。

我希望这有帮助,请原谅我的英语不太好

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-

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-

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.

I hope this helps and excuse my englis which is not very good

顾铮苏瑾 2024-08-13 01:45:39

我也遇到了同样的问题,结果发现是一个错字。
我的错误消息是:

Unknown column 'departure' in 'where clause'

我检查了表中的这一列,结果发现,我在表中将其拼写为“出发”而不是“出发”,因此抛出错误消息。

我随后将查询更改为:

Unknown column 'depature' in 'where clause' 

并且它有效!

所以我的建议明确是,仔细检查您是否正确拼写了列名称。

我希望这有帮助。

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.

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.

我不会写诗 2024-08-13 01:45:39

从 linux (bash) 命令行执行以下命令时,我还遇到了“where 子句中的未知列”的问题。

mysql -u support -pabc123 -e 'select * from test.sku where dispsku='test01' ; '

这就是我得到的结果,

ERROR 1054 (42S22) at line 1: Unknown column 'test01' in 'where clause'

我必须将单引号 'test01' 替换为双引号 "test01" 。这对我有用。执行 sql 查询的方式和方式有所不同。

当在脚本中为变量赋值时,以及稍后在必须由脚本执行的 sql 语句中使用该变量时,存在细微差别。

如果假设变量是

var=testing

并且您想将该值从脚本内传递到 mysql,那么单引号就可以了。

select '$var'

因此,不同的引擎可能会以不同的方式评估反引号和引号。

这是我从 linux 命令行运行的查询。

mysql -u support -pabc123 -e 'select * from test.sku where dispsku="test01" ; '

I also faced the issue of "Unknown column in where clause" when executing the following from linux (bash) command line.

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.

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

var=testing

and you want to pass this value from within script to mysql, then single quotes work.

select '$var'

So different engines might evaluate backticks and quotes differently.

This is my query that worked from linux command line.

mysql -u support -pabc123 -e 'select * from test.sku where dispsku="test01" ; '
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文