On every individual node page of a Drupal 7.0 site, I get the following Notice:

Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 724 of /[site-path]/modules/comment/comment.module).

I've been unable to associate it with any particular instances other than the fact that it appears on every Node page (not on front page, taxonomy term pages, View pages, etc).

Comments

bfroehle’s picture

scottrouse’s picture

Thanks. Neither Google or D.O's search found that for me.

bfroehle’s picture

I found it by searching for 'comment_count' on the Drupal project issue page. http://drupal.org/project/issues/drupal?text=comment_count :)

Hope you can figure out a solution for the bug.

manumazu’s picture

Hello, it happens when node_comment_statistics table is empty. I had this problem after adding content directly in node content : the trigger that is counting comments is disable.
You can verify if this table is empty or not, or a relation between nid in node table and nid in node_comment_statistics is not broken.
This query works fine :

INSERT INTO `node_comment_statistics` (`nid`, `cid`, `last_comment_timestamp`, `last_comment_name`, `last_comment_uid`, `comment_count`)
SELECT nid, 0, UNIX_TIMESTAMP(), NULL, 1, 0 FROM node

Hope it can help you ...

kosmik’s picture

I too was manually uploading contents into drupal 7 tables from drupal 6. Everything seemed to work ok except I was seeing a similar error notice as you guys.

After many many trials, I noticed I have to fully populate table "node_comment_statistics" for "comment_count" even for those without comments. Just enter node id and "0" for comment count for all nodes with no comments.

Hope this helps!

amirtaiar’s picture

I have been trying to followup but didn't really understand what to do.
Can you be more detailed please?
Thank you!

xcel’s picture

The query given by manumazu worked fine for me.
Thanks a lot !

davidwhthomas’s picture

This SQL query will also rebuild comment statistics and fixes the error, in Drupal 7

TRUNCATE TABLE node_comment_statistics;
INSERT INTO
    node_comment_statistics
(
    nid,
    last_comment_timestamp,
    last_comment_name,
    last_comment_uid,
    comment_count
)
SELECT
    n.nid,
    IFNULL(last_comment.created,n.changed) AS last_comment_timestamp,
    IFNULL(last_comment.name,null) AS last_comment_name,
    IFNULL(last_comment.uid,n.uid) AS last_comment_uid,
    IFNULL(comment_count.comment_count,0) AS comment_count
FROM
    node AS n
    LEFT OUTER JOIN (SELECT nid, COUNT(*) AS comment_count FROM comment WHERE status=1 GROUP BY nid) AS comment_count ON comment_count.nid=n.nid
    LEFT OUTER JOIN (SELECT nid, MAX(cid) AS max_cid FROM comment WHERE status=1 GROUP by nid) AS max_node_comment ON max_node_comment.nid=n.nid
    LEFT OUTER JOIN (SELECT cid,uid,name,created FROM comment ORDER BY cid DESC LIMIT 1) AS last_comment ON last_comment.cid=max_node_comment.max_cid
WHERE
    n.status=1
ORDER BY
    n.nid; 

Credit: http://drupal.org/node/137458

HTH.

DT

franklca’s picture

The SQL query in comment #8 works like magic! Thank you for posting.

johnnydarkko’s picture

#8 was perfect for me. Thanks @davidwhthomas!

W.M.’s picture

@davidwhthomas

Thanks very much.. solved 2 issues for me

akki123’s picture

#8 worked like charm.
Thanks

betarobot’s picture

David, thank you so much! Was totally puzzled, but #8 worked for me perfectly as well.

Zanel’s picture

#8 works for me, but only temporarily.
I find that I have to apply it at least daily...

Is there a permanent fix? What could be causing this error to repeatedly occur? Failing that, can this be put into a PHP file (PHP NOOB here) that can be called by cron?

Thanks!

tohidd’s picture

Thank you for sql query on comment 8. it saved my ass

revez2002’s picture

Issue summary: View changes

This issue had me scratching my head, but after running the SQL query posted by davidwhthomas (comment#8), everything is working like a charm.

Thanks David!

func0der’s picture

Please, be aware that the query from comment#8 does NOT save last commentator of a node correctly.

To fix such issues simply run the cron manually via the backend or drush cron via the terminal.
That should fix this issue, too, and update the last commentator of the nodes.

libbyy’s picture

I tried running the cron but I still get the following error:

Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 728 of /usr/www/users/XXX/extranet/modules/comment/comment.module).

can anyone advise?

drupix’s picture

I had the same problem with a custom module who create custom node.
After changing the weight of my module to "1" (higer than comment.module) the problem disapear for new custom content created.

To resolve the problem for custom nodes created before I change the weight of my modules, I had to use the script #8.
Now, everything works fine.

func0der’s picture

Since there is no proper way to set the weight of a module from installation on, I would say that A solution, but not THE solution for the cause of the problem.

electrokate’s picture

#8 query still working wonders, thanks!

jorisx’s picture

Just ran in the same weird error..
But #8 Fixed it! Many thanks!!!

maikeru’s picture

#8 won't for those using postgres, instead replace ifnull with coalesce.

oduranv’s picture

davidwhthomas's solution works for me. Thanks.

Regards.

ElZipo’s picture

I just disable comment module... I'll replace facebook comment or disqus comment

blogers’s picture

#8 works for me, orfanat nodes missed in table node_comment_statistics, with #8 fixed all table thanks

shomari’s picture

Still an issue today and #8 worked like a charm.

vchen’s picture

#8 still works.

For anyone needing extra instructions, you can run the entire SQL query with drush sqlc. Wait until you see the mysql> prompt, then copy and paste the entire SQL query and hit enter. Get out of mysql by typing exit.

Also, running this did not delete my comments.

UPDATE: This actually didn't work on the server (php 7.3.11). I did notice that this happens primarily on unpublished pages while logged in, at least from my experience. This notice did not pop up when logged out. It had worked for my local instance on php 7.3.9.