papaface Posted April 29, 2007 Share Posted April 29, 2007 Hello, How can I count how many records are in a table that match a news_id? I have this at the moment, but it doesn't seem to work. <?php $_getcomments = mysql_query("select count(comment_id) from comments where news_id='{$news_id}'"); list($__num) = mysql_fetch_array($_getcomments); echo $__num; ?> Comments</div> I get: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /***/index.php on line 48 Comments Quote Link to comment https://forums.phpfreaks.com/topic/49177-solved-count-records/ Share on other sites More sharing options...
tauchai83 Posted April 29, 2007 Share Posted April 29, 2007 try this: </php $_getcomments = mysql_query("SELECT count(comment_id) AS cnt FROM comments where news_id='$news_id' "); echo $cnt; ?> if it is not work, add GROUP BY comment_id Quote Link to comment https://forums.phpfreaks.com/topic/49177-solved-count-records/#findComment-240970 Share on other sites More sharing options...
papaface Posted April 29, 2007 Author Share Posted April 29, 2007 That works in a way. I don't get an error now. But I dont get the number that it should be. It should say there is 1 record in the database with news_id, but it doesnt print anything. Quote Link to comment https://forums.phpfreaks.com/topic/49177-solved-count-records/#findComment-240972 Share on other sites More sharing options...
tauchai83 Posted April 29, 2007 Share Posted April 29, 2007 Alternatively, <?php $_getcomments = mysql_query("SELECT count(comment_id) AS cnt FROM comments where news_id='$news_id' "); $num=count(mysql_num_rows($_getcomments)); echo $num; ?> now try this. haha. keep on trying. Quote Link to comment https://forums.phpfreaks.com/topic/49177-solved-count-records/#findComment-240973 Share on other sites More sharing options...
papaface Posted April 29, 2007 Author Share Posted April 29, 2007 Now I get: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /***/index.php on line 52 0Comments Quote Link to comment https://forums.phpfreaks.com/topic/49177-solved-count-records/#findComment-240975 Share on other sites More sharing options...
tauchai83 Posted April 29, 2007 Share Posted April 29, 2007 check your column name and also ehco out the variable. see what you get? $news_id value is passed? Quote Link to comment https://forums.phpfreaks.com/topic/49177-solved-count-records/#findComment-240977 Share on other sites More sharing options...
tauchai83 Posted April 29, 2007 Share Posted April 29, 2007 you should try this: <?php $sql="SELECT * FROM comments where news_id='$news_id' "; $result=mysql_query($sql); $count=mysql_num_rows($result); echo $count; ?> it should work! Quote Link to comment https://forums.phpfreaks.com/topic/49177-solved-count-records/#findComment-240979 Share on other sites More sharing options...
papaface Posted April 29, 2007 Author Share Posted April 29, 2007 lol I just found out the problem. I have multiple databases running on this script and I forgot to reference the connection lol. Sorry about that, and thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/49177-solved-count-records/#findComment-240984 Share on other sites More sharing options...
tauchai83 Posted April 29, 2007 Share Posted April 29, 2007 welcome. Quote Link to comment https://forums.phpfreaks.com/topic/49177-solved-count-records/#findComment-240989 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.