0

i am not sure if i am doing this correctly, and i am kinda stucked here.

Here's what i want to do, each row in the database has 5 column, say A,B,C,D with each having an integer value out of 10 and E which is like a counter. so what i want is to get an average.

which should be something like below

(A + B + C + D)/(E * 4)

and each of these values calculated above, has to be summed up and divide by the total number of rows.

So say i have 3 entry it has to divided by 3

so what i came up with is

$myresult = mysql_query("SELECT * FROM studies WHERE classes = '$classid'");
list($mycount) = mysql_fetch_row($myresult);
$result = mysql_query("SELECT ID, sum((maths + sciences + moral + bm)/(count*4))/'$mycount' FROM studies WHERE classes = '$classid'");

am i doing this right?

if the sql is right, how am i suppose to print out the result so i can use it as a variable?

thank you

1
  • column needs alias: SELECT id, sum((maths + .........)) as 'avg' ... so you will have avg row keys in your fetched array Commented Sep 14, 2013 at 15:01

1 Answer 1

1

The query should be right, but to get the actual value is easy. You have to fetch_array the query, and the first variable would be the ID and the second would be the sum.

Example

while($row = mysql_fetch_array($query)){
echo $row[0]; // This is the ID
echo $row[1]; // This is the sum
}
Sign up to request clarification or add additional context in comments.

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.