SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    IN
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php mysql error Warning: Division by zero in

    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;
    }

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $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.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    IN
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So...........any suggestions as how this can be solved.......as I am totally novice for this field..

    Thanks!

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'll give it a shot, though I'm making some guesses at what you're trying to do with the code:

    PHP 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";


  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    IN
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its working perfectly dan...........................thanks a lot..........

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?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;
    }

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •