I have tried to make this work but it adds up all the rows not just 5 like I am trying to in the query
$username = $_SESSION['username'];
$stmt = $db->prepare("
select sum(correct) from exams where
username = :username ORDER BY :testID DESC LIMIT :5");
Due to the problems with not being able to callthe last 5 rows I am unable to test the ordering string.
I have read questions similar to this on stackoverflow but still cant seem to get it right. maybe there are other problems with my code but I can always add that if requsted :)
the full code
<?php
require('includes/config.php');
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }
$username = $_SESSION['username'];
$last5rate = $db->prepare("select sum(correct) from exams where username = :username ORDER BY :testID DESC LIMIT 5");
$last5rate->execute(array(':username' => $username));
for($i=0; $rows = $last5rate->fetch(); $i++){
//Edit this row
$last5 = $rows['sum(correct)'];
$last5final = $last5 / 10;
}
echo $last5final;
?>
if you are not binding limit then change your query
select sum(correct) from exams where username = :username ORDER BY :testID DESC LIMIT :5
$last5rate->execute(array(':username' => "$username"));
for($i=0; $rows = $last5rate->fetch(); $i++){
$last5 = $rows['sum(correct)'];
}
to
select sum(correct) as correct from exams where username = :username ORDER BY :testID DESC LIMIT 5
$last5rate->execute(array(':username' => $username,':testID' => $testID));
while ($row = $last5rate->fetch(PDO::FETCH_ASSOC)) {
echo $row['correct']; // or use it as you want
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.