[support] Printing "0" when a db query returns nothing

Ivan Sergio Borgonovo mail at webthatworks.it
Fri Apr 9 17:58:36 UTC 2010


On Fri, 9 Apr 2010 13:49:43 -0300
"Neil Coghlan" <neil at esl-lounge.com> wrote:

> I am trying to print the number of downloads a user's attachments
> have produced and am 95% there. I just don't know how to print
> zero if the SUM returns nothing.

I'd check why a sum() is returning null before casting in php.

sum() is going to return a 0 if NO record was returned.
That means there is at least one count that is null (and some may
not be), a null in a sum is enough to nullify the sum. 10 + null =
null.

select sum(coalesce(count, 0))...
You'd be better to set default 0 on files_download.count and update
the null record to 0.

-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it



More information about the support mailing list