Jump to content

Finding things in db that are filled in


runnerjp

Recommended Posts

 if ($result = mysql_query("SELECT COUNT(lastposter) as cnt, SUM(numreplies) as sm FROM forumtutorial_posts WHERE `forum` = 'general' ")) {
                      if (mysql_num_rows($result)) {
                         $row   = mysql_fetch_assoc($result);
                         $topic = $row['cnt'];
                         $posts = $row['cnt'] + $row['sm'];
                 ?>

 

Im trying to get num of topics in my forum

 

in my db it looks like this

 

forum        Lastposter    views

general          Admin          3

general                              0

 

as you can see there is only 1 result in lastposter but for some reason when i get the results it echo 2 instead of 1... why?

Link to comment
https://forums.phpfreaks.com/topic/198743-finding-things-in-db-that-are-filled-in/
Share on other sites

COUNT doesn't care if the Lastposter column is empty. It only counts the number of rows selected and since you didn't specify that Lastposter equaling to the empty string shouldn't be selected, COUNT will of course count it as a result. If you want to only have 1 result, you would need to specify in your query to not select the rows where Lastposter is equal to the empty string.

 

SELECT COUNT(*) AS cnt, SUM(numreplies) AS sm FROM forumtutorial_posts WHERE forum = 'general' AND Lastposter <> '';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.