MYSQL PHP获得单个字段的平均值 [英] MYSQL PHP getting average of a single field
本文介绍了MYSQL PHP获得单个字段的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我制作了一个表格,允许用户提交自己的姓名,评论和1-6分,然后将其保存在各自字段的表格中;名称,评论和分数.我不会显示平均分数.
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 wan't to display the average score.
这是我到目前为止发现的:
This is what I've found out so far:
$result = mysql_query("SELECT AVG(fieldName) FROM tableName");
我该如何回应呢?
推荐答案
为您的结果提供别名,使访问变得更容易.
Give your result an alias, It makes accessing it easier.
使用 mysql_fetch_assoc()
获得结果
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'];
仅供参考,mysql_*
已过时.尝试使用PDO或mysqli.
FYI, mysql_*
is obsolete. Try PDO or mysqli instead.
这篇关于MYSQL PHP获得单个字段的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文