Jump to content

Recommended Posts

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

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);

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());

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.