elmas156 Posted December 3, 2012 Share Posted December 3, 2012 Hello, I'm creating a "comments" section for my website, and I want to display a few of my most recent comments posted by users. I know how I would display ALL of the comments posted, but not sure how I would display only a few... say 4 or 5 comments. Anyone have any suggestions or ideas? Here's my current code: <?php $resultcomm = mysql_query("SELECT `name`,`comment` FROM comments ORDER BY `index` DESC") or die (mysql_error()); while ($rowcomm = mysql_fetch_row($resultcomm)) { $name = $rowcomm[0]; $comment = $rowcomm[1]; echo "<p class='p14'>$comment<br />"; echo "<div class='p15'>- $name</div></p>"; echo "<hr width='200' />"; } ?> Thanks for any help! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 3, 2012 Share Posted December 3, 2012 To limit the number of results returned, you would add a LIMIT clause to your query. Quote Link to comment Share on other sites More sharing options...
elmas156 Posted December 3, 2012 Author Share Posted December 3, 2012 Just as a guess, to limit the search to 5 results, would I use the LIIMIT clause in this manner? I'm not near my computer with my code right now, so I can't test it for myself... $resultcomm = mysql_query("SELECT `name`,`comment` FROM comments ORDER BY `index` DESC LIMIT 5") or die (mysql_error()); Thanks again for your help. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 3, 2012 Share Posted December 3, 2012 No need to guess. You can simply read the manual for that statement and figure out exactly how it works: http://dev.mysql.com/doc/refman/5.5/en/select.html The part on LIMIT is about 1/10 the way down the page. Quote Link to comment Share on other sites More sharing options...
elmas156 Posted December 4, 2012 Author Share Posted December 4, 2012 That was easy. Thanks! 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.