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

Link to comment
Share on other sites

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.