I searched internet and stack-overflow for better logic I could work with but could not find. The error of the code is syntax error which I could not figure out. I am newbie and not well familiar with pdo or mysqli way. So I used deprecated mysql. As of now the print_r() command does not output anything as there is syntax error. Where could I go wrong? Please help me.
$sub = "SELECT id FROM subjects WHERE class_id = 5 GROUP BY name";
$subj = mysql_query($sub,$dbconnect);
$rows = array();
while ($row = mysql_fetch_assoc($subj)) {
$rows[] = $row;
}
$i=1;
$query_res = "SELECT regd,";
foreach($rows as $sub):
$query_res .= " SUM(CASE WHEN `subject` IN ('".$sub['id']."')";
$query_res .= " AND entry IN ('1')";
$query_res .= " THEN (mark_score) END)/";
$query_res .= " SUM(CASE WHEN `subject` IN ('".$sub['id']."')";
$query_res .= " AND entry IN ('1')";
$query_res .= " THEN (full_mark) END)*100 AS subj$i";
endforeach;
$query_res .= " FROM $dbexam WHERE regd='1' AND session='6'";
$res = mysql_query($query_res, $dbconnect) or die(mysql_error());
$row_res = mysql_fetch_assoc($res);
print_r($row_res);
Here is the actual error:
You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'SUM(CASE
WHEN `subject` IN ('16') AND entry IN ('1') THEN (mark_score)
END)/ SU' at line 1