无法弄清楚如何从MySql Query

时间:2020-04-17 04:34:46

标签: php html mysql

我早些时候问过一个有关如何执行此查询的问题,并收到了很好的答复。不幸的是,它比我通常运行的查询要先进一些,因此我无法弄清楚如何检索结果并将其显示在我的网页上。这是原始问题original question

的链接
     <?php

// Associative array
$result = $db->query("SELECT Name,
       COALESCE(AVG(CASE WHEN mth = 1 THEN PTS END), 0) AS Jan,
       COALESCE(AVG(CASE WHEN mth = 2 THEN PTS END), 0) AS Feb,
       COALESCE(AVG(CASE WHEN mth = 3 THEN PTS END), 0) AS Mar,
       COALESCE(AVG(CASE WHEN mth = 4 THEN PTS END), 0) AS Apr,
       COALESCE(AVG(CASE WHEN mth = 5 THEN PTS END), 0) AS May,
       COALESCE(AVG(CASE WHEN mth = 6 THEN PTS END), 0) AS Jun,
       COALESCE(AVG(CASE WHEN mth = 7 THEN PTS END), 0) AS Jul,
       COALESCE(AVG(CASE WHEN mth = 8 THEN PTS END), 0) AS Aug,
       COALESCE(AVG(CASE WHEN mth = 9 THEN PTS END), 0) AS Sep,
       COALESCE(AVG(CASE WHEN mth = 10 THEN PTS END), 0) AS Oct,
       COALESCE(AVG(CASE WHEN mth = 11 THEN PTS END), 0) AS Nov,
       COALESCE(AVG(CASE WHEN mth = 12 THEN PTS END), 0) AS Dec,
       AVG(PTS) AS AVG
FROM (
  SELECT Name, `Points Pass` AS PTS, MONTH(STR_TO_DATE(`OS Date`, '%a %b %e %H:%i:%s %Y')) AS mth
  FROM data
) d
GROUP BY Name");


while($row = mysqli_fetch_array($result)) {

            ?>

         <tr>
                <td><?php echo $row['Name']; ?></td>
                <td class = "gScore-<?php echo $row['Jan']; ?>"><?php echo $row['Jan']; ?></td>
                <td class = "gScore-<?php echo $row['Feb']; ?>"><?php echo $row['Feb']; ?></td>
                <td class = "gScore-<?php echo $row['Mar']; ?>"><?php echo $row['Mar']; ?></td>
                <td class = "gScore-<?php echo $row['Apr']; ?>"><?php echo $row['Apr']; ?></td>
                <td class = "gScore-<?php echo $row['May']; ?>"><?php echo $row['May']; ?></td>
                <td class = "gScore-<?php echo $row['Jun']; ?>"><?php echo $row['Jun']; ?></td>
                <td class = "gScore-<?php echo $row['Jul']; ?>"><?php echo $row['Jul']; ?></td>
                <td class = "gScore-<?php echo $row['Aug']; ?>"><?php echo $row['Aug']; ?></td>
                <td class = "gScore-<?php echo $row['Sep']; ?>"><?php echo $row['Sep']; ?></td>
                <td class = "gScore-<?php echo $row['Oct']; ?>"><?php echo $row['Oct']; ?></td>
                <td class = "gScore-<?php echo $row['Nov']; ?>"><?php echo $row['Nov']; ?></td>
                <td class = "gScore-<?php echo $row['Dec']; ?>"><?php echo $row['Dec']; ?></td>
                <td><?php echo $row['AVG']; ?></td>
            </tr>
        <?php   


}
if (!$result) { echo $db->error; }

1 个答案:

答案 0 :(得分:1)

您遇到了一些问题:

  1. NewClass的索引必须用引号引起来,例如function NewClass(staticField: string): DynamicClass_ { class DynamicClass { ... } let proto: any = DynamicClass.prototype; proto.staticField = staticField; return DynamicClass; } ,否则会收到很多“未定义的常量”警告;
  2. $row是检索月度数据的正确方法,您需要在其他每个月中复制一次;
  3. $row['name']替换为$row['Jan']
  4. 我不确定您要使用
  5. 实现什么
$avgGrade

你真的想要

$row['AVG']
  1. 您需要将<td class = "gScore-<?php echo $row[Jan]; ?>"></td> 行添加到查询中,即
<td class = "gScore"><?php echo $row[Jan]; ?></td>