You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP使用While循环读取MySQL关联表展示成绩的问题求助

问题原因及解决方案

现有代码存在的核心问题

  • SQL语法错误:关联条件中表名和字段名拼写错误,你定义的科目表名为subjects、对应关联字段为subjecttype,但你写的条件是subject.subjectstype,表名少了s、字段多了s,首先会导致查询失败。
  • PHP语法错误:输出科目名称的行echo "<td>". $row['name']; "</td>";中间错误使用分号打断语句,会触发语法报错。
  • 行输出逻辑错误:你现在的逻辑是每读取一条成绩记录就输出一整个<tr>行,所以每个成绩自然会单独占一行,没有做同科目类型的成绩归集。
  • 序号固定错误:每行的序号都写死为1,所有行序号都会显示为1,不符合使用逻辑。

实现同一科目类型成绩同行的方案

方案1:SQL层面先聚合成绩(更简便)

使用GROUP_CONCAT函数先将同一subjecttype的所有成绩合并为单个字段再输出,修正后的完整代码如下:

<?php
// 修正SQL拼写错误,新增分组聚合逻辑
$sqltest1 = "SELECT 
    subjects.id, 
    subjects.name, 
    subjects.subjecttype, 
    GROUP_CONCAT(grades.grade SEPARATOR '、') as all_grades 
FROM grades 
INNER JOIN subjects ON grades.subjecttype = subjects.subjecttype 
GROUP BY subjects.subjecttype, subjects.id, subjects.name";
?>
<table class="table table-bordered">
  <thead>
    <tr>
      <th class="col-1">#</th>
      <th class="col-3 text-center">Subject</th>
      <th class="col-6 text-center">Grade</th>
      <th class="col-1 text-center">Options</th>
    </tr>
  </thead>
  <tbody>
<!-- ###################################################################### -->
  <?php
  $result = $conn->query($sqltest1);
  if ($result->num_rows > 0) {
    $index = 1;
    // 输出数据
    while($row = $result->fetch_assoc()) {
      echo "<tr>";
      echo "<td scope='row'>".$index."</td>";
      echo "<td>". $row['name']. "</td>";
      echo "<td>". $row["all_grades"]."</td>";
      echo "<td class='text-center'>Option</td>";
      echo "</tr>";
      $index++;
  }
}
?>
<!-- ###################################################################### -->
</tbody>
</table>

方案2:PHP层面归集数据

如果需要对单条成绩做单独处理,可以先把查询结果按subjecttype分组后再遍历输出,示例逻辑如下:

<?php
// 先修正SQL拼写错误
$sqltest1 = "SELECT * FROM grades, subjects WHERE grades.subjecttype = subjects.subjecttype ORDER BY subjects.subjecttype";
$result = $conn->query($sqltest1);
$subjectData = [];
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // 按subjecttype归集成绩
        $st = $row['subjecttype'];
        if(!isset($subjectData[$st])){
            $subjectData[$st] = [
                'name' => $row['name'],
                'grades' => []
            ];
        }
        $subjectData[$st]['grades'][] = $row['grade'];
    }
}
?>
<!-- 表格部分遍历$subjectData即可 -->
<table class="table table-bordered">
  <thead>
    <tr>
      <th class="col-1">#</th>
      <th class="col-3 text-center">Subject</th>
      <th class="col-6 text-center">Grade</th>
      <th class="col-1 text-center">Options</th>
    </tr>
  </thead>
  <tbody>
  <?php
  $index = 1;
  foreach($subjectData as $item){
      echo "<tr>";
      echo "<td scope='row'>".$index."</td>";
      echo "<td>". $item['name']. "</td>";
      // 多个成绩用顿号拼接,也可以自定义格式
      echo "<td>". implode('、', $item['grades'])."</td>";
      echo "<td class='text-center'>Option</td>";
      echo "</tr>";
      $index++;
  }
 ?>
  </tbody>
</table>

内容的提问来源于stack exchange,提问作者Zimnyjestem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 23:24:05