I have made a function in old mysql and now want to transfer it to PDO, but its not working. Here is my new code:
global $host, $dbname, $user, $pass;
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$STH = $DBH->query("SELECT SUM(score), SUM(score_from) FROM school_test_report, school_students
WHERE (school_test_report.student_id = school_students.student_id
and school_test_report.class=school_students.class)
and school_test_report.student_id = '$student_id' and school_test_report.subject = '$subject'
and school_test_report.test_date >= '$thisarch'
")
$STH->setFetchMode(PDO::FETCH_ASSOC);
return $STH;
and it outputs:
$student_id = test_score_month($name, 'English');
echo $student_id['score'].'/'.$student_id['score_from'];
Here is old code which is working:
$result1 = mysql_query("SELECT SUM(score), SUM(score_from) FROM school_test_report, school_students
WHERE (school_test_report.student_id = school_students.student_id
and school_test_report.class=school_students.class)
and school_test_report.student_id = '$student_id' and school_test_report.subject = '$subject'
and school_test_report.test_date >= '$thisarch'
")
or die(mysql_error());
$row = mysql_fetch_assoc($result1);
return $row;
and it outputs:
$student_id = test_score_month($name, 'English');
echo $student_id['score'].'/'.$student_id['score_from'];
Heres is an example, wrap all your functions todo with your querys related to your users in a model class and then call each method to return your result ect, see how the function/method uses placeholders
:student_idthen bindParam’s the values to thos placeholders, it is just an example but will help you understand abit more.