Forum Moderators: coopster

Message Too Old, No Replies

If you do a count() and the result is nothing, what's the value?

Such as counting number of comments for a particular topic...

         

HughMungus

3:56 am on Mar 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a database table with a number of topics and a separate table with comments related to each topic by id number. I'm trying to setup a thing to display the number of comments for each topic, but, when I get to topics that have no comments at all, I'm not getting zero, I'm getting...not sure. What am I getting and how do I test for it so I can set it to zero?

In other words, what is the result set of a count() that returns nothing because there is nothing to count?

ironik

4:10 am on Mar 23, 2005 (gmt 0)

10+ Year Member



If there are no results for the query then nothing will be returned. You can check if there are a certain number of rows returned using mysql_num_rows(), if your number of rows is 0, then assign the variable the value 0, if it's 1 or greater then you can access the count() function in the query to return your count.

If you are just returning the number of rows then you should probably just use mysql_num_rows() anyway...

HughMungus

4:15 am on Mar 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops. I just realized how dumb this question was after I re-read it.

Thanks for the answer! I'll use your method (which I knew but had forgotten).

By the way, are there other ways to indicate that no results were returned based on a mysql query?

ironik

4:41 am on Mar 23, 2005 (gmt 0)

10+ Year Member



msql_num_rows() is the only function that I know off the top of my head. Another possible way is storing the count as an interation when you process the rows...

tkroll

5:16 am on Mar 23, 2005 (gmt 0)

10+ Year Member



Hello,

I think the original question was what does a query such as this return:

select count(id) from posts where something=1;

This query will always return a single row with a number. If there are no matching rows you will get 0.

HughMungus

7:23 pm on Mar 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, something like that, TK. But what was happening was that some articles have no comments in the comments table so, therefore, looking for comments for article number 1 (for example) that has no comments will return zero rows (not zero or null).