dachshund Posted August 30, 2009 Share Posted August 30, 2009 hi, i'm just trying to count the amount of comments on an article but it's not working. can anyone see what's wrong? i have a connection etc etc. <?php $id=mysql_real_escape_string($_GET['id']); echo mysql_query("SELECT COUNT(*) FROM comments WHERE articleid='$id'"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/172463-solved-count-entries/ Share on other sites More sharing options...
trq Posted August 30, 2009 Share Posted August 30, 2009 mysql_query returns a result resource. You will need to pass that resource to mysql_fetch_row. Quote Link to comment https://forums.phpfreaks.com/topic/172463-solved-count-entries/#findComment-909236 Share on other sites More sharing options...
dachshund Posted August 30, 2009 Author Share Posted August 30, 2009 so something like this? $id=mysql_real_escape_string($_GET['id']); $commentresult=mysql_query("SELECT COUNT(*) FROM comments WHERE articleid='$id'"); $commentrow=mysql_fetch_row($commentresult); echo $commentrow; ?> that just returns 'Array' though. Quote Link to comment https://forums.phpfreaks.com/topic/172463-solved-count-entries/#findComment-909242 Share on other sites More sharing options...
MasterACE14 Posted August 30, 2009 Share Posted August 30, 2009 $id=mysql_real_escape_string($_GET['id']); $commentresult=mysql_query("SELECT COUNT(*) FROM comments WHERE articleid='$id'"); $commentrow = mysql_num_rows($commentresult); echo $commentrow; Quote Link to comment https://forums.phpfreaks.com/topic/172463-solved-count-entries/#findComment-909246 Share on other sites More sharing options...
dachshund Posted August 30, 2009 Author Share Posted August 30, 2009 that's what i originally thought but for some reason that always returns '1'. even when i just change it to $commentresult=mysql_query("SELECT COUNT(*) FROM comments"); it returns 1. then i deleted all the entries in that table, and still it returns 1. Quote Link to comment https://forums.phpfreaks.com/topic/172463-solved-count-entries/#findComment-909248 Share on other sites More sharing options...
.josh Posted August 30, 2009 Share Posted August 30, 2009 that's because mysql_num_rows tells you how many rows were returned from your query. Since you are only selecting count(*) you are only returning 1 row. You need to use mysql_result or mysql_fetch_array or mysql_fetch_assoc or mysql_fetch_row. You tried fetch_row and got "array" because fetch_row returns an array of all the columns of the row. In this particular instance, since you are only selecting count(*), you would echo $commentrow[0] after fetch_row Quote Link to comment https://forums.phpfreaks.com/topic/172463-solved-count-entries/#findComment-909255 Share on other sites More sharing options...
dachshund Posted August 30, 2009 Author Share Posted August 30, 2009 thanks! Quote Link to comment https://forums.phpfreaks.com/topic/172463-solved-count-entries/#findComment-909260 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.