acidglitter Posted January 13, 2008 Share Posted January 13, 2008 This is what I have.. $sql=mysql_query("SELECT *, (SELECT COUNT(*) FROM news_comments WHERE news_id=id) AS 'numcomments' FROM news ORDER BY id DESC") or die(mysql_error()); And for comments, if there aren't any it says 0, and if there's one or more it always says 1. Like its just showing true or false.. Does anyone see whats wrong with my code? Because I want it to show the *actual* number of comments Link to comment https://forums.phpfreaks.com/topic/85819-solved-subquery-problem/ Share on other sites More sharing options...
Fyorl Posted January 13, 2008 Share Posted January 13, 2008 I'm not 100% sure here as I'm not really familiar with what you're doing in that query but will the following not achieve the comment count: $sql = " SELECT COUNT(*) FROM `news_comments` WHERE `news_id`='$id' "; $data = mysql_query($sql); $numcomments = mysql_result($data, 0); Link to comment https://forums.phpfreaks.com/topic/85819-solved-subquery-problem/#findComment-437990 Share on other sites More sharing options...
GingerRobot Posted January 13, 2008 Share Posted January 13, 2008 The problem is that you havn't specified which table the field id should be in, inside your subquery. Try: $sql=mysql_query("SELECT *, (SELECT COUNT(*) FROM news_comments WHERE news_id=news.id) AS 'numcomments' FROM news ORDER BY id DESC") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/85819-solved-subquery-problem/#findComment-438056 Share on other sites More sharing options...
acidglitter Posted January 17, 2008 Author Share Posted January 17, 2008 that fixed it. thanks! Link to comment https://forums.phpfreaks.com/topic/85819-solved-subquery-problem/#findComment-441980 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.