I'm running a query to get the total sum of some hours from a database table to show in a PDF file but for some strange reason it won't echo 0 (zero).
$query = mysql_query("SELECT id, SUM(hours_night), SUM(hours_days) FROM table WHERE invoiceID='".mysql_real_escape_string($invoiceID)."'") or die(mysql_error());
$result = mysql_num_rows($query);
$totalhours_night = 0;
while ($fetch = mysql_fetch_assoc($query)) {
$totalhours_night += $fetch['SUM(hours_night)'];
}
$html_output = "Some html and tables markup... " . $totalhours_night . "";
The output of the html works fine, thats not the problem.. but the problem is it won't output 0 (zero) for some reason. If the hour result is actualy something like 1 or 5 or whatever it outputs the totalhours fine, but i need it to output 0 if there are no hours.
Because it looks strange to ouput nothing if there are no hours, i need to display a 0 zero since this looks more good.
BTW, if i for example put number_format($totalhours_night, 2); it does display 0.00, but i need it to be just 0.
Treat integer as string like this-
echo (string)$totalhours_night;
Three things:
id
and SUM(..)
unless you use GROUP BY id
(Which kid of loses the idea if id
is unique)SUM(..)
give it an alias like SUM(..) AS alias1
, and later in the PHP refer to alias1
.var_dump($fetch['alias1']);
and look at what you really get back. (And post your findings here so we can give you further help)