0

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"); 

How do I echo this out?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked, thanks! $result = mysqli_query($con,"SELECT AVG(fieldName) AS avg FROM tableName"); while($row = mysqli_fetch_array($result)) { echo $row['avg']; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.