0

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);
?>

3 Answers 3

1

just change your query to this

"SELECT studentid,hw1, hw2, hw3, SUM(hw1+hw2+hw3) as hw
FROM grade
GROUP BY studentid"
Sign up to request clarification or add additional context in comments.

Comments

0

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>";
}

1 Comment

Thanks everyone. Sorry for my poor written English above. All suggestions are correct.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.