MySQL Forums
Forum List  »  PHP

Re: Adding up results
Posted by: Roland Bouman
Date: July 28, 2005 02:09PM

Ok, if the intention is to get the grand total of the score (rAID?) for every single question (rQID?) in the questionnaire per user (rUID? why is your php variabe named $aID), you could do it in one query:

$sql = 'select sum(rAID) score_total, count(rQID) number_of_answers, count(distinct rQID) number_of_questions'
. 'from qResults'
. 'where rUID = '.$aID
$conn = $this->getConnection();
$result = mysql_query($sql,$conn);
$data = mysql_fetch_assoc($result);
//maybe check if tha number of questions/answers is indeed 20 as you expect
$score = $data['score_total'];
}else {
//whoops, no such set of answered questions
//deal with it
return $score;

so, instead of looping through all questions, and summing them manually, you use an aggregate function and do it all in one go.

Options: ReplyQuote

Written By
Re: Adding up results
July 28, 2005 02:09PM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.