如何每周通过UserID汇总表列并在PHP中将其回显

时间:2018-08-29 23:54:38

标签: php mysql

我需要帮助,以便能够从星期日开始按用户ID每周汇总“ total_attendance表列”,并进行回显。

本周的输出应覆盖上周的输出,而下周的输出应像这样反复输入本周的输出

下面是我已经为此编写的代码

我收到此错误。

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,布尔值在…中给出。

除了错误,我还需要一个有效的代码。

一如既往的感谢

<?php $result = mysqli_query($con, "SELECT SUM(totalattn) FROM 
attendance GROUP BY yearweek(date, 0) WHERE user_id='$user_id'");

 while ($row = mysqli_fetch_assoc($result)) {
 $totalattn = $row['totalattn']; 
 echo $totalattn;
 }                            
?>   

1 个答案:

答案 0 :(得分:1)

GROUP BY子句要求您指定作为SELECT条款一部分进行分组的字段。 另外,WHERE子句需要放在GROUP BY子句

之前
<?php $result = mysqli_query($con, "SELECT yearweek(date, 0), SUM(totalattn) FROM 
attendance  WHERE user_id='$user_id' GROUP BY yearweek(date, 0)");

始终遵循以下顺序:

SELECT something
FROM some place
JOIN with some other place
WHERE conditions are met
GROUP BY something you selected which you'd want to aggregate

此外,请记住不要执行查询操作,以防止注射和各种令人讨厌的情况