Hi,
I am getting division by zero error in php…please help.
Here’s the code.
$biz_val= $row_c[‘biz_id’];
$sql_rat=“select * from tblrating where rat_biz_id = “.$row_c[‘biz_id’].””;
$result_rat = mysql_query($sql_rat);
$tot_rec = mysql_fetch_array($result_rat);
$i=$j=$d=0;
//$row_rt = 0;
if($tot_rec>0)
{ $row_rt = $tot_rec;
while ($i=mysql_fetch_array($result_rat))
{
$i++;
$j= $row_rt[‘rat_num’] + $j;
}
$d=$j/$i;
if ($d<=1.50)
{
echo img1.gif;
}
if ($d<=2.50 && $d>=1.51)
{
echo img2.gif;
}
}
else
{
echo img0.gif;
}
$tot_rec = mysql_fetch_array($result_rat);
$i=$j=$d=0;
//$row_rt = 0;
if($tot_rec>0)
I don’t think this does what you think it does… $tot_rec contains an array corresponding to a row from the result set. You then try to compare it to 0, but it’s an array, not a number.
So…any suggestions as how this can be solved…as I am totally novice for this field…
Thanks!
I’ll give it a shot, though I’m making some guesses at what you’re trying to do with the code:
$biz_val = $row_c['biz_id'];
$sql = "SELECT * FROM tblrating WHERE rate_biz_id = $biz_val";
$result = mysql_query($sql) or die("Error in SQL: " . mysql_error());
$count = 0;
$sum = 0;
$average = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$sum += $row['rat_num'];
$count++;
}
}
if ($count > 0) {
$average = $sum / $count;
}
if ($average <= 1.5) {
echo "img1.gif";
} else if ($average <= 2.5) {
echo "img2.gif";
} else {
echo "img0.gif";
}
Its working perfectly dan…thanks a lot…
alandy
March 13, 2010, 2:51pm
6
<?php
$i=$j=$d=0;
$biz_val= $row_c[‘biz_id’];
if($biz_val>0){
$sql_rat="select * from tblrating where rat_biz_id = ".$row_c[‘biz_id’];
$result_rat = mysql_query($sql_rat);
while ($rs=mysql_fetch_array($result_rat)){
$i++;
$j+= $rs['rat_num'];
}
}
if($i>0){
$d=$j/$i;
if ($d<=1.50 && $d>0){
echo img1.gif;
}
if ($d<=2.50 && $d>1.50){
echo img2.gif;
}
}else{
echo img0.gif;
}
?>