im having a bit of a problem with my php script that counts the id column entries that match a certain questionnaire id. The results seem to be quite antisocial and just dont want to group up for some reason....
$sql30 = <<<SQL
SELECT id, COUNT(id)
FROM `QuestionnaireAnswers`
WHERE questionnaireID='$questionnaireID'
GROUP BY id
SQL;
if(!$result30 = $db->query($sql30)){ die('There was an error running the query [' . $db->error . ']');}
while($row30 = $result30->fetch_assoc()){
if ($row30['COUNT(id)'] == '' OR $row30['COUNT(id)'] == '0'){$numberofresponses = '0';}
else {$numberofresponses = $row30['COUNT(id)'];}
echo '<td>'.$numberofresponses.'</td>';
}
If I use a sample questionnaireID of 1327809154 and run the following query in phpmyadmin :
SELECT id, COUNT( id )
FROM `QuestionnaireAnswers`
WHERE questionnaireID = '1327809154'
GROUP BY id
LIMIT 0 , 30
I get the following results :
id COUNT(id)
129 1
130 1
131 1
132 1
133 1
134 1
277 1
278 1
280 1
281 1
282 1
284 1
290 1
419 1
424 1
425 1
426 1
so.....it appears to be counting the results, but not grouping them to give me a total.........
EDIT,I have now changed it to :
$sql30 = <<<SQL
SELECT questionnaireID, COUNT(questionnaireID)
FROM `QuestionnaireAnswers`
WHERE questionnaireID='$questionnaireID'
GROUP BY questionnaireID
SQL;
if(!$result30 = $db->query($sql30)){ die('There was an error running the query [' . $db->error . ']');}
while($row30 = $result30->fetch_assoc()){
if ($row30['COUNT(questionnaireID)'] == '' OR $row30['COUNT(questionnaireID)'] == '0'){$numberofresponses = '0';}
else {$numberofresponses = $row30['COUNT(questionnaireID)'];}
echo '<td>'.$numberofresponses.'</td>';
}
This returns :
questionnaireID COUNT(questionnaireID)
1327809154 17
this works in phpmyadmin, but some reason nothing is returned in my script, the table column where the count should be is blank.