catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

sum() sometimes returns string instead of number #1083

Closed m-nienberg closed 9 months ago

m-nienberg commented 1 year ago

If the sum() functions doesn't find any matches it returns an empty string. I suggest it would be better to return zero. Otherwise you can get a fatal error if you try to use the result in a function that will not accept a string.

$total = $database->sum("account", "money", ["currency" => "dollars"]); 
echo "We have $" . number_format($total, 0);   <--- fatal error

The documentation at https://medoo.in/api/sum says that the return will be a number. I haven't tested, but the same may be true for the other Aggregation functions.

catfan commented 1 year ago

That's the document problem. I have fixed it. 4cb6410

Those aggregate methods will return a string. You have to cast it to float or integer. Zero will not be correct in some cases.