Reputation: 41
How can I calculate the average of column values in a mySQL table and display it in an HTML table?
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$sql = 'SELECT AVG(price) FROM emp';
$retval = mysql_query( $sql, $conn );
$values = mysql_num_rows($retval);
echo $values;
It returns 1. I feel like the problem is with mysql_num_rows() but what will be the correct code to display the average output?
Upvotes: 0
Views: 837
Reputation: 2956
$sql = 'SELECT AVG(price) FROM emp';
$retval = mysql_query( $sql, $conn );
$values = mysql_num_rows($retval);
echo $values;//it will display the number of rows of your resultant table
$avg=mysql_result($retval,0);
echo $avg;//it will show the average
Dont use mysql_* as they are depracated.time to move on to mysqli_* or PDO.
Upvotes: 1