unemployment Posted March 8, 2011 Share Posted March 8, 2011 function fetch_feedback_best() { $sql = "SELECT `like` FROM `feedback` GROUP BY `page` asc LIMIT 10"; while($row = mysql_fetch_assoc($sql)) { $results[] = $row; } return $results; } This doesn't work. My error is "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /blah.inc.php on line 105 ". <?php $results = fetch_feedback_best(); foreach ($results as $result) { ?> echo $result['page']; } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 8, 2011 Share Posted March 8, 2011 You're never performing the query, you're only defining it. Try: <?php function fetch_feedback_best() { $sql = "SELECT `like` FROM `feedback` GROUP BY `page` asc LIMIT 10"; $rs = mysql_query($sql); while($row = mysql_fetch_assoc($rs)) { $results[] = $row; } return $results; } ?> Ken Quote Link to comment Share on other sites More sharing options...
unemployment Posted March 8, 2011 Author Share Posted March 8, 2011 Thank you... That worked, but I have another question. In MySQL I have a table with two columns. page likes profile 1 profile 1 index 1 company 1 company 1 company 1 company 1 I want to display the top ten best performing pages in order with my group by... It's not display correctly though function fetch_feedback_top_performers() { $sql = "SELECT `page`, COUNT(`like`) FROM `feedback` GROUP BY `page` ORDER BY `like` asc LIMIT 10"; $rs = mysql_query($sql); while($row = mysql_fetch_assoc($rs)) { $results[] = $row; } return $results; } So essentially, this should read Company (4) Profile (2) Index (1) Quote Link to comment Share on other sites More sharing options...
DavidAM Posted March 8, 2011 Share Posted March 8, 2011 Your ORDER BY is ascending, so you are getting the least liked pages first. Change the ORDER BY to DESCending to get the most liked pages first. $sql = "SELECT `page`, COUNT(`like`) FROM `feedback` GROUP BY `page` ORDER BY `like` DESC LIMIT 10"; Quote Link to comment Share on other sites More sharing options...
sasa Posted March 8, 2011 Share Posted March 8, 2011 you mean ORDER BY COUNT(`like`) Quote Link to comment Share on other sites More sharing options...
DavidAM Posted March 8, 2011 Share Posted March 8, 2011 Oops, good catch sasa. Those backticks always screw with my head. I usually just alias the expression and order by the alias (and I never use backticks): SELECT page, COUNT(like) AS Cnt FROM feedback GROUP BY page ORDER BY Cnt DESC LIMIT 10; Quote Link to comment 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.