function examResult($student_id, $exam_id){
global $conn;
$stmt = $conn->prepare("SELECT SUM(CASE WHEN answers.answered_option=questions.correct_option THEN questions.marks ELSE 0 END) AS obtain, exams.exam_name, exams.total_marks, exams.pass_marks FROM exams INNER JOIN questions ON exams.id=questions.exam_id INNER JOIN answers ON answers.question_id=questions.id WHERE answers.student_id = ? AND exams.id = ? ");
if($stmt){
return 'ok';
$stmt->bind_param('ii', $student_id, $exam_id);
$stmt->execute();
$result = $stmt->get_result();
if($result){
$row = $result->fetch_assoc();
return $row;
}
} else{
return false;
}
}
I was expecting 'ok' from the above function but it returning false. That means the $stmt is not executing. Also my both parameters $student_id & $exam_id are fine.
But if I execute it in my SQL console like below it works perfectly.
SELECT SUM(CASE WHEN answers.answered_option=questions.correct_option THEN questions.marks ELSE 0 END) AS obtain, exams.exam_name, exams.total_marks, exams.pass_marks FROM exams INNER JOIN questions ON exams.id=questions.exam_id INNER JOIN answers ON answers.question_id=questions.id WHERE answers.student_id = 18 AND exams.id = 24