close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Display average of database entries?

Thread began 12/15/2009 9:22 pm by ijk76319396 | Last modified 12/16/2009 12:54 pm by Jason Byrnes | 1720 views | 5 replies |

ijk76319396

Display average of database entries?

I have users rate each page on a scale of 1 to 5. I would like for the average rating to be displayed on the page. I have already created a recordset which has the table filtered by the user's ID. How can I display the average rating?

Sign in to reply to this post

Jason ByrnesWebAssist

The SQL AVG() function to can used to get the average from a column:

SELECT AVG(rating) AS averageRating FROM table
Sign in to reply to this post

ijk76319396

Can you check that this query is correct, when I go to open it in the bindings panel I get the message MySQL Error#: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table
WHERE 0 = 1' at line 1


$query_rsAVGRATING = sprintf("SELECT AVG(rating) AS averageRating FROM table WHERE ID = %s", GetSQLValueString($colname_rsAVGRATING, "int"));
$rsAVGRATING = mysql_query($query_rsAVGRATING, $con2com) or die(mysql_error());
$row_rsAVGRATING = mysql_fetch_assoc($rsAVGRATING);
$totalRows_rsAVGRATING = mysql_num_rows($rsAVGRATING);
Sign in to reply to this post

ijk76319396

Fixed it, I left the value as 'table' instead of inserting the name of my table.

Sign in to reply to this post

ijk76319396

Another issue

Another issue came up with some of the averages being way to long in size, how can I limit the average to be displayed to only two decimals, for example instead of showing 2.24333333, have it cut off at 2.24?

Sign in to reply to this post

Jason ByrnesWebAssist

use the MySQL FORMAT function:
FORMAT(number to format, number of decimal places)

for 2 decimal places:

SELECT FORMAT(AVG(rating), 2) AS averageRating FROM table
Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...