我的代码如下: -
$sql="SELECT student_id,DA1,DA2,DA3,DA4,DA5,DA6,HA1,HA2,HA3,HA4,HA5,HA6 from table";
$results = $result->query($sql);
while($row = $results->fetch_assoc())
{
$id = $row['student_id'];
$marks1 = $row['DA1'] + $row['HA1']/2 ;
$marks2 = $row['DA2'] + $row['HA2']/2 ;
$marks3 = $row['DA3'] + $row['HA3']/2 ;
$marks4 = $row['DA4'] + $row['HA4']/2 ;
$marks5 = $row['DA5'] + $row['HA5']/2 ;
$marks6 = $row['DA6'] + $row['HA6']/2 ;
$i = 1;
while($i <= 6)
{
$sql = "SELECT `grade`,`point` FROM `grades` where ${'marks' . $i} BETWEEN min and max";
$results = $result->query($sql);
$row = $results->fetch_assoc();
${'grade' . $i} = $row['grade'];
${'point' . $i} = $row['point'];
$i++;
}
$totalcredit = 20;
$sgpa= ($point1*$c1 + $point2*$c2 + $point3*$c3 + $point4*$c4 + $point5*$c5 + $point6*$c6) / $totalcredit ;
$sql = "UPDATE table SET `G1` = '$grade1', `G2` = '$grade2' ,`G3` ='$grade3',`G4` = '$grade4',`G5` = '$grade5',`G6` = '$grade6', `SGPA` = '$sgpa' WHERE student_id = '$id'";
$result->query($sql);
}
在这段代码中,我试图计算一个班级的成绩和sgpa。在第一个查询中选择所有细节并计算六个科目中的总分数。在第二个查询中,我将等级和点存储在相对于标记的变量中,然后计算sgpa。然后执行更新查询以将详细信息存储在数据库中。
该表包含100行。代码适用于第一行,但不适用于其他行并返回空值。我递归地试图用所有学生的数据来计算和更新表格。如果有人可以帮助运行所有行的代码,我将非常感激。
答案 0 :(得分:1)
更改内部while循环,如下所示: -
$i = 1;
while($i <= 6)
{
$sql = "SELECT `grade`,`point` FROM `grades` where ${'marks' . $i} BETWEEN min and max";
$results2 = $result->query($sql);
$row = $results2->fetch_assoc();
${'grade' . $i} = $row['grade'];
${'point' . $i} = $row['point'];
$i++;
}
在你的代码中$结果被覆盖,因此在内循环中将其更改为$ results2将解决您的问题。