MYSQL PHP getting average of a single field

后端 未结 1 701
忘掉有多难
忘掉有多难 2021-01-27 10:07

I\'ve made a form that allows users to submit their name, a comment and a score from 1-6, which then is saved in a table in their respective fields; name, comment and score. I

相关标签:
1条回答
  • 2021-01-27 11:00

    Give your result an alias, It makes accessing it easier.

    Use mysql_fetch_assoc() to get your results

    $result = mysql_query("SELECT AVG(fieldName) AS avg FROM tableName");
    $row = mysql_fetch_assoc($result);
    echo $row['avg'];
    

    FYI, mysql_* is obsolete. Try PDO or mysqli instead.

    0 讨论(0)
提交回复
热议问题