Reputation: 3
My question is that why there are blank values for HW1/HW2/HW3 columns when I ran the code in the browser?.
Studentid and Sum columns displayed the code correctly. Any ideal how to fix this?
<?php
$result = mysqli_query($con,"SELECT studentid,SUM(hw1+hw2+hw3)
FROM grade
GROUP BY studentid");
echo "<table border='1'>
<tr>
<th>StudentID</th>
<th>HW1</th>
<th>HW2</th>
<th>HW3</th>
<th>SUM</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['studentid'] . "</td>";
echo "<td>" . $row['hw1'] . "</td>";
echo "<td>" . $row['hw2'] . "</td>";
echo "<td>" . $row['hw3'] . "</td>";
echo "<td>" . $row['SUM(hw1+hw2+hw3)'] . "</td>";
;}
echo "</table>";
mysqli_close($con);
?>
Upvotes: 0
Views: 232
Reputation: 276
just change your query to this
"SELECT studentid,hw1, hw2, hw3, SUM(hw1+hw2+hw3) as hw
FROM grade
GROUP BY studentid"
Upvotes: 1
Reputation: 953
Because you did not select the columns, try:
$result = mysqli_query($con, 'SELECT `studentid`, `hw1`, `hw2`, `hw3`, SUM(`hw1`+`hw2`+`hw3`) as `sum` FROM `grade` GROUP BY `studentid`');
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['studentid'] . "</td>";
echo "<td>" . $row['hw1'] . "</td>";
echo "<td>" . $row['hw2'] . "</td>";
echo "<td>" . $row['hw3'] . "</td>";
echo "<td>" . $row['sum'] . "</td>";
}
Upvotes: 0
Reputation: 1131
I'm not sure, since your question is poorly written. But I think you just need to do this to get the values:
SELECT studentid,hw1,hw2,hw3,SUM(hw1+hw2+hw3)
Upvotes: 0