I've looked at the other 15 or so answers related to this and haven't come up with an answer that works. Here's the code responsible:
function count_distinct($table, $field, $from, $match, $other='Other'){
try{
$ret = array();
$query = 'SELECT '.$table.'.'.$field.', COUNT(*) AS num FROM '.$from;
$query .= ($match) ? ' WHERE '.$match : '';
$query .= ' GROUP BY `'.$table.'`.`'.$field.'`';
$q = mysqli_query($this->con, $query);
if (!$q) {
throw new Exception('Error executing query ' . $query . ' ' . mysqli_error($this->con));
} else {
...
}
return $ret;
}
catch(Exception $e){... }
}
I've removed some of the processing code which seems to work fine. The inputs for this function are code-generated, not client/user generated, and are free from white space or symbols.
I'm getting this error:
Error executing query SELECT interventions.iTYPE, COUNT(*) AS num
FROM interventions JOIN INC ON INC.form_id = interventions.form_id JOIN FORMS ON INC.form_id=FORMS.form_id WHERE `interventions`.`iCAT` = "IV/IO" AND INC.`Pt.AgeRange` <> "Manikin/Lab" AND FORMS.course_id="15" GROUP BY `interventions`.`iTYPE` Unknown column 'interventions.iTYPE' in 'field list'
The table config is this:
interventions
form_id (int (10))
iCAT (varchar(20))
iTYPE (varchar(20))
Result (tinyint(1))
INC
form_id (int(10))
Pt.AgeRange (varchar(20))
FORMS
form_id (int(10))
course_id (int(20))
etc.
The crazy part is that if I copy/paste the query printed in the error message and run it in Sequel Pro it works fine, error free, and prints the result without a problem.
I'm at my wits end—I've tried restarting the server, changing the order of the field list, removing the GROUP BY clause, adding back ticks, removing them...I'm out of ideas.