1

My query is throwing up this error. Can anyone see why?

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

Unknown column '' in 'where clause'

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
George Reith
  • 13,132
  • 18
  • 79
  • 148

4 Answers4

5

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
    
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

Because apparently $uniqueUnits[a] resolves to the empty string. And there is no column like this in the database.

DoubleMalt
  • 1,263
  • 12
  • 17
0

Try surrounding your array with {}, like this:

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

Also, is column ID actually in your table?

Bojangles
  • 99,427
  • 50
  • 170
  • 208