我正在尝试从数据库中获取数据并添加一个名为 marks 的特定行
我成功地从数据库中获取了数据,在获取的数据中,有一个名为标记的列,现在我试图获取列中值的总和,但是当我运行代码时,它只显示第一个值 这是我的代码
<?php
$select = "select subjectName, marks, studentId from tbl_subject, tbl_results, tbl_student where tbl_subject.id = tbl_results.SubjectId and studentId = tbl_results.stdntsId and studentId = $id";
// $select = "select from tbl_results";
$execute = mysqli_query($conn, $select);
$countRows = mysqli_num_rows($execute);
if($countRows > 0){
while($fetch = mysqli_fetch_array($execute)){
$marks = $fetch['marks'];
?>
<tbody>
<tr>
<th scope="row">
<?php echo $fetch['subjectName'] ?>
</th>
<td>
56
</td>
<td><?php echo $marks ?></td>
<td>D1</td>
<td>Good</td>
<td>O.B</td>
</tr>
</tbody>
<?php
}
?>
<tr>
<th>TOTAL</th>
<td>
34
</td>
<td>
<?php
$total_qty = 0;
// $a = $fetch['marks'];
$total_qty += $marks;
echo $total_qty;
?>
</td>
<td><b>AGGR: <span>5</span></b></td>
</tr>
<?php
}
?>
</table>
<table>
<tbody>
<?php
$select = "select t1.subjectName, t2.marks, t3.studentId "
. "from "
. "tbl_subject t1, "
. "tbl_results t2, "
. "tbl_student t3"
. "WHERE "
. "t1.id = t2.SubjectId "
. "AND "
. "t2.studentId = t3.stdntsId "
. "AND "
. "t1.studentId = $id";
$execute = mysqli_query($conn, $select);
$countRows = mysqli_num_rows($execute);
if($countRows > 0){
$count = 0;
$total_qty = 0;
while($fetch = mysqli_fetch_array($execute)){
$count++;
$marks = $fetch['marks'];
$total_qty += $marks;
?>
<tr>
<td scope="row"><?=$fetch['subjectName'] ?></td>
<td>56</td>
<td><?=$marks ?></td>
<td>D1</td>
<td>Good</td>
<td>O.B</td>
</tr>
<?php
}
?>
<tr>
<td>TOTAL</td>
<td>34</td>
<td><?=$count;?> or <?=$total_qty;?></td>
<td><b>AGGR: <span>5</span></b></td>
</tr>
<?php
}
?>
</tbody>
</table>
我认为您的查询有问题,请尝试将您正在查询的表标记为 t1、t2 等
放
<tbody>
外站循环
如果你想显示重复次数你需要在循环之前声明
$count = 0
然后在循环内增加它$count++
和总和
$total_qty += $marks;
你需要把它放在一个循环中。
希望有帮助