我的 php 脚本有点问题,该脚本计算与某个问卷 id 匹配的 id 列条目。结果似乎很反社会,只是出于某种原因不想分组....
$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>';
}
如果我使用 1327809154 的示例问卷 ID 并在 phpmyadmin 中运行以下查询:
SELECT id, COUNT( id )
FROM `QuestionnaireAnswers`
WHERE questionnaireID = '1327809154'
GROUP BY id
LIMIT 0 , 30
我得到以下结果:
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
所以.....它似乎在计算结果,但没有将它们分组给我一个总数............
编辑,我现在将其更改为:
$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>';
}
这返回:
questionnaireID COUNT(questionnaireID)
1327809154 17
这在 phpmyadmin 中有效,但由于某种原因,我的脚本中没有返回任何内容,计数应该为空的表列是空白的。