简体   繁体   中英

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'

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.

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


You should do the following:

  • Interpolate your variables explictly with the "complex syntax" to be sure that the string forms properly;

  • Check the value of $query so that you can see what's going on:

     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.

Because apparently $uniqueUnits[a] resolves to the empty string. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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