如何计算mySQL表中列值的平均值并将其显示在HTML表格中?
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$sql = 'SELECT AVG(price) FROM emp';
$retval = mysql_query( $sql, $conn );
$values = mysql_num_rows($retval);
echo $values;
它返回1.我觉得问题出在mysql_num_rows()上,但是显示平均输出的正确代码是什么?
答案 0 :(得分:1)
$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
不要使用mysql_ *,因为它们是depracated.time以转移到mysqli_ *或PDO。