starvinmarvin14 Posted February 5, 2012 Share Posted February 5, 2012 I have a table which holds each individual rating as a row. An example of some data would be: picid | rating 12 2 43 3 12 4 36 7 43 9 I also have another table which holds image info and has a column with the id that matches picid on the first table. How would I find the average rating of each 'picid' to be displayed on a top rated page? So it should output picid | rating 36 7 43 6 12 3 So far I have... $statement1 = "SELECT picid, SUM(rating) AS total FROM ratings GROUP BY picid ORDER by total"; Let me know if you need further clarification. Quote Link to comment https://forums.phpfreaks.com/topic/256447-selecting-highest-rating-from-list-of-ratings/ Share on other sites More sharing options...
fenway Posted February 5, 2012 Share Posted February 5, 2012 You can add AVG(rating) without incident. Quote Link to comment https://forums.phpfreaks.com/topic/256447-selecting-highest-rating-from-list-of-ratings/#findComment-1314756 Share on other sites More sharing options...
starvinmarvin14 Posted February 5, 2012 Author Share Posted February 5, 2012 How would I call this query? I am using mysql_fetch_assoc and am getting an error. Is there anything wrong with this code? $statement1 = "SELECT picid FROM ratings GROUP by picid ORDER by AVG(rating)"; $row2 = mysql_fetch_assoc($statement1); Quote Link to comment https://forums.phpfreaks.com/topic/256447-selecting-highest-rating-from-list-of-ratings/#findComment-1314798 Share on other sites More sharing options...
fenway Posted February 5, 2012 Share Posted February 5, 2012 Are you going to make us _guess_ the error? Quote Link to comment https://forums.phpfreaks.com/topic/256447-selecting-highest-rating-from-list-of-ratings/#findComment-1314848 Share on other sites More sharing options...
starvinmarvin14 Posted February 5, 2012 Author Share Posted February 5, 2012 Sorry... Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/user/public_html/mysite/toprated.php on line 52 The query is failing. Quote Link to comment https://forums.phpfreaks.com/topic/256447-selecting-highest-rating-from-list-of-ratings/#findComment-1314862 Share on other sites More sharing options...
fenway Posted February 6, 2012 Share Posted February 6, 2012 You marked this "solved", yet I don't see the solution. Quote Link to comment https://forums.phpfreaks.com/topic/256447-selecting-highest-rating-from-list-of-ratings/#findComment-1315108 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.