P.Bes P.Bes - 1 year ago 67
PHP Question

Find Average with mysql_fetch_array and array_sum

I have this php file and I want to find the average of each column(sum the m1 column for example and divide with $total to take the average) but with the code below nothing happens whta is the proble?



$select_rating=mysql_query("select m1,m2,m3,m4,m5,m6,m7,m8,m9,m10 from rating");




<form method="post" action="">

<div id="div1" class="div">
<p>Charlie and the Chocolate Factory (<?php echo $total_m1_rating;?>)</p></div>

/the same for m2,m3,m4.../

Answer Source

You can use AVG() function like this..

$select_rating=SELECT Avg(m1) as avg_m1, Avg(m2) as avg_m2, Avg(m3) as avg_m3, Avg(m4) as avg_m4, Avg(m5) as avg_m5, Avg(m6) as avg_m6, Avg(m7) as avg_m7, Avg(m8) as avg_m8, Avg(m9) as avg_m9, Avg(m10) as avg_m10 FROM rating;


<?php echo $m1; ?>

This will return you average of each column. For more you can try this link http://www.w3schools.com/sql/sql_func_avg.asp

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download