jokerfool Posted August 23, 2012 Share Posted August 23, 2012 I have a table named 'comments', and I would like to use PHP to return to the browser how many comments are already stored within the database. Cheers Quote Link to comment Share on other sites More sharing options...
spiderwell Posted August 23, 2012 Share Posted August 23, 2012 http://php.net/manual/en/function.mysql-num-rows.php Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 If all you need is the number of comments on a post, use COUNT(). SELECT COUNT(comment_id) AS total_comments FROM comments WHERE post_id = x Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted August 23, 2012 Share Posted August 23, 2012 You can also use SQL_CALC_FOUND_ROWS if you're paginating the results. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 You can also use SQL_CALC_FOUND_ROWS if you're paginating the results. +1 Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted August 23, 2012 Share Posted August 23, 2012 SQL_CALC_FOUND_ROWS won't work on ultra-large result sets, like billions of rows. It's best to use some other sort of counting/estimation tactic separate from your query for that. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2012 Share Posted August 23, 2012 If you want the number of rows in a table use "SELECT COUNT(*) FROM tablename". Mysql does not have to read every row to get this value as it would if you were to use mysql_num_rows(). Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted August 23, 2012 Share Posted August 23, 2012 If you want the number of rows in a table use "SELECT COUNT(*) FROM tablename". Mysql does not have to read every row to get this value as it would if you were to use mysql_num_rows(). Only true for certain storage engines which store row_count in the table's metadata. IIRC, it works instantly on MyISAM, but slower on InnoDB. But the OP was asking for number of comments per topic anyway, I think. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2012 Share Posted August 23, 2012 He just wants to know how many comments are stored in the database Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted August 24, 2012 Share Posted August 24, 2012 He just wants to know how many comments are stored in the database I figured he didn't actually mean "total comments in the table," but we may never know. 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.