尝试从这里获取多列的平均值,但所有值都打印为0

时间:2014-02-06 23:52:01

标签: php mysql arrays average

这是我的代码,我的错误是$ result变量应该是一个数组。但我不太清楚如何实现这一点。

<?php     
        $sql ="SELECT AVG(dive_rating) AS avg_rating, 
        AVG(col1) AS col1,
        AVG(col2) AS col2,
        AVG(col3) AS col3,
        AVG(col4) AS col4,
        FROM this_table WHERE col_name='$col_name'";

        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);

        $col1 = $row['col1'];
        $col2 = $row['col2'];
        $col3 = $row['col3'];
        $col4 = $row['col4'];
        $col5 = $row['col5'];

        echo round($col1,1);
        echo round($col2,1);
        echo round($col3,1);
        echo round($col3,1);
?>

1 个答案:

答案 0 :(得分:2)

你有一个额外的逗号,请尝试这样:

$sql ="SELECT AVG(dive_rating) AS avg_rating, 
        AVG(col1) AS col1,
        AVG(col2) AS col2,
        AVG(col3) AS col3,
        AVG(col4) AS col4
        FROM this_table WHERE col_name='$col_name'";