简体   繁体   English

'where子句'中的未知列''

[英]Unknown column '' in 'where clause'

My query is throwing up this error.我的查询抛出了这个错误。 Can anyone see why?谁能明白为什么?

$query = "SELECT * FROM Units WHERE ID = `$uniqueUnits[a]`";

Unknown column '' in 'where clause' 'where子句'中的未知列''

Two problems.两个问题。

  • You're using backticks to delimit a string.您正在使用反引号来分隔字符串。 Backticks delimit fields , so MySQL thinks you're trying to give it a column name.反引号分隔字段,因此 MySQL 认为您正在尝试给它一个列名。

  • The error message indicates that, in fact, this value that it thinks is a column name, is empty.错误消息表明,实际上,它认为是列名的这个值是空的。 So your value $uniqueUnits[a] is probably broken, or not being interpolated correctly.因此,您的价值$uniqueUnits[a]可能已损坏,或者未正确插值。


You should do the following:您应该执行以下操作:

  • Interpolate your variables explictly with the "complex syntax" to be sure that the string forms properly;使用“复杂语法”明确地插入变量,以确保字符串 forms 正确;

  • Check the value of $query so that you can see what's going on:检查$query的值,以便您可以看到发生了什么:

     print $query;
  • Use actual quotation marks to delimit strings:使用实际的引号来分隔字符串:

     $query = "SELECT * FROM Units WHERE ID = '{$uniqueUnits[a]}'"; // ^ quote // ^ PHP variable interpolation

try尝试

$query = "SELECT * FROM Units WHERE ID = '$uniqueUnits[a]'";
                                         ^---            ^---

Backticks are for escaping reserved words, so mysql is translating your variable's contents into a field name.反引号用于 escaping 保留字,因此 mysql 将变量的内容转换为字段名称。

Because apparently $uniqueUnits[a] resolves to the empty string.因为显然 $uniqueUnits[a] 解析为空字符串。 And there is no column like this in the database.并且数据库中没有这样的列。

Try surrounding your array with {} , like this:尝试用{}包围您的数组,如下所示:

$query = "SELECT * FROM Units WHERE ID = `{$uniqueUnits[a]}`";

Also, is column ID actually in your table?另外,列ID实际上在您的表中吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM