[thelist] strange php/mysql display problem
Bob Meetin
bobm at dottedi.biz
Mon Jun 7 23:52:37 CDT 2010
This seems really simple, yet causes heartache.
I am using a javascript program called flot to build charts and
php/mysql to feed data. Almost everything works but when I run a very
very simple mather operation the chart fails.
$query = "select count(rating) as num_ratings, sum(rating) as
sum_ratings from some_table " ;
$result = mysql_query ($query);
$number_results = mysql_num_rows ($result);
if(mysql_num_rows($result)> 0)
{
$row = mysql_fetch_array($result);
$sum_ratings = $row["sum_ratings"]; // the result here is "15"
$num_ratings = $row["num_ratings"]; // the result is "7"
# $avg_rating = $sum_ratings / $num_ratings; // this causes failure
in the display
$avg_rating = $sum_ratings / 7; // this works
$avg_rating = number_format($avg_rating,1,".",",");
}
The failure is really peculiar. I have to comment out the mathematical
equation for the chart to display, not just usage of $avg_rating, but
then of course I lose automation in the calculations. For whatever
reason the page does not like using $num_ratings as the denominator, but
will allow the number 7. They are equivalent.
I tried some conversion which hasn't helped yet:
$num_ratings = intval(trim($num_ratings));
So, what is the problem with using the variable in the denominator?
-Bob
More information about the thelist
mailing list