Warning: Division by zero

Discussion in 'PHP' started by newphpcoder, Nov 22, 2010.

  1. newphpcoder

    newphpcoder New Member

    Joined:
    Sep 24, 2010
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Programmer
    Location:
    Philippines
    Good day I got a warning like this :
    Warning: Division by zero in C:\Documents and Settings\LT\report.php on line 122
    Warning: Division by zero in C:\Documents and Settings\LT\report.php on line 220
    I have this code in line 22:
    PHP:
    $yield = ($c_output $f_input) * 100;
    and this is my code in line 220
    PHP:
    $yield = ($sol_output $buff_input) * 100;
    my c_output is = 65.17
    f_input is = 68.40
    so the result should be : 95.27 but on my output is 0.00%
    and my sol_output = 0.00
    buff_input = 0. 00
    and the result is 0.00 % which is correct but why I got this warning.

    Thank you
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,367
    Likes Received:
    386
    Trophy Points:
    83
    Warning is on line 122 and not on 22
     
  3. newphpcoder

    newphpcoder New Member

    Joined:
    Sep 24, 2010
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Programmer
    Location:
    Philippines

    Yup...for further understand here is my full code:

    PHP:
    if($_POST["start_date"]) 
            { 
            
    $query "SELECT plt_no FROM plt_transact WHERE plt_date = '" $_POST["start_date"] . "'"
            
    $result_loop mysql_query($query); 
             
            
    $Batch_yield 0
            
    $buff_input 0
            
    $sol_output 0
            
    $loopctr 0
            
    $totalloop 0
             
            
    /***Header****/ 
            
    if($result_loop) if(mysql_num_rows($result_loop) > 0
                { 
                
    $totalloop mysql_num_rows($result_loop); 
                 
                
    $plt_no mysql_result($result_loop,0,"plt_no"); 
                
    $query "SELECT d.operation_name, SUM(d.input_qty) AS inputqty, d.input_unit, SUM(d.output_qty) AS outputqty, d.output_unit FROM traceability d, plt_transact t WHERE t.plt_no = '$plt_no' AND d.plt_transact_id = t.plt_transact_id GROUP BY d.operation_name, d.output_unit, d.input_unit ORDER BY d.operation_name"
                
    $result mysql_query($query); 
                if(
    $result
                    { 
                    echo 
    "<table cellspacing='2' style='font-family: arial narrow; font-size: 12px; border-width: 2px 2px 2px 2px; border-style: solid;'>"
                     
                    echo 
    "<tr>"
                    echo 
    "<th class='tdclass'>PLT #</th>"
                    
    $total_row mysql_num_rows($result); 
                    for(
    $ctr=0$ctr $total_row$ctr++) 
                        { 
                        
    $opname mysql_result($result,$ctr,"operation_name"); 
                        
    $i strpos($opname," ",0); 
                        
    $opname substr($opname,$i); 
                        echo 
    "<th colspan='2' class='tdclass'>" $opname "<br />(" mysql_result($result,$ctr,"output_unit") . ")</th>"
                        } 
                    echo 
    "<th class='tdclass'>PLT Yield</th>"
                    
    //echo "<th class='tdclass'>Batch Yield</th>"; 
                    
    echo "</tr>"
                     
                    echo 
    "<tr>"
                    echo 
    "<td class='tdclass'></td>"
                     
                    for(
    $ctr=0$ctr $total_row$ctr++) 
                        { 
                        echo 
    "<td class='tdclass'>Input</td>"
                        echo 
    "<td class='tdclass'>Output</td>"
                        } 
                     
                    echo 
    "<td class='tdclass'>"
                    echo 
    "</td>"
                         
                    echo 
    "</tr>"
                             
                    } 
                } 
             
            
    $query "SELECT plt_no FROM plt_transact WHERE plt_date = '" $_POST["start_date"] . "'"
            
    $result_loop mysql_query($query);                
            while(
    $row mysql_fetch_array($result_loop)) 
                { 
                
    $loopctr += 1
                
    $plt_no $row["plt_no"]; 
                
    $query "SELECT * FROM plt_transact WHERE plt_no = '$plt_no'"
                
    $result_no mysql_query($query); 
                if(
    $result_no
                    { 
                    if(
    mysql_num_rows($result_no) > 0
                        { 
                        
    $f_input 0
                        
    $c_output 0
                         
                        
    $query  "SELECT SUM(t.input_qty) AS f_input FROM traceability t, plt_transact p WHERE t.plt_transact_id = p.plt_transact_id AND t.operation_name='05 Forming' AND p.plt_no = '$plt_no'"
                        
    $resultyield mysql_query($query); 
                        if(
    $resultyield
                            { 
                            if(
    mysql_num_rows($resultyield) > 0$f_input mysql_result($resultyield,0,"f_input"); 
                            } 
                         
                        
    $query  "SELECT SUM(t.output_qty) AS c_output FROM traceability t, plt_transact p WHERE t.plt_transact_id = p.plt_transact_id AND t.operation_name='08 Core Molding' AND p.plt_no = '$plt_no'"
                        
    $resultyield mysql_query($query); 
                        if(
    $resultyield
                            { 
                            if(
    mysql_num_rows($resultyield) > 0$c_output mysql_result($resultyield,0,"c_output"); 
                            } 
                             
                        
    $PLT_yield = ($c_output $f_input) * 100
                    
                         
                        
    $query  "SELECT SUM(t.input_qty) AS buff_input FROM traceability t, plt_transact p WHERE t.plt_transact_id = p.plt_transact_id AND t.operation_name='09 Core Buffing, Insp and Sizing' AND p.plt_no = '$plt_no'"
                        
    $resultyield mysql_query($query); 
                        if(
    $resultyield
                            { 
                            if(
    mysql_num_rows($resultyield) > 0$buff_input $buff_input mysql_result($resultyield,0,"buff_input"); 
                            } 
                         
                        
    $query  "SELECT SUM(t.output_qty) AS sol_output FROM traceability t, plt_transact p WHERE t.plt_transact_id = p.plt_transact_id AND t.operation_name='10 Core Solutioning' AND p.plt_no = '$plt_no'"
                        
    $resultyield mysql_query($query); 
                        if(
    $resultyield
                            { 
                            if(
    mysql_num_rows($resultyield) > 0$sol_output $sol_output mysql_result($resultyield,0,"sol_output"); 
                            } 
                         
                        
    $query "SELECT d.operation_name, SUM(d.input_qty) AS inputqty, d.input_unit, SUM(d.output_qty) AS outputqty, d.output_unit FROM traceability d, plt_transact t WHERE t.plt_no = '$plt_no' AND d.plt_transact_id = t.plt_transact_id GROUP BY d.operation_name, d.output_unit, d.input_unit ORDER BY d.operation_name"
                        
    $result mysql_query($query); 
                        if(
    $result
                            { 
                            
                             
                            echo 
    "<tr>"
                            echo 
    "<td><strong>$plt_no</strong></td>"
                             
                            for(
    $ctr=0$ctr $total_row$ctr++) 
                              { 
                                echo 
    "<td class='tdclass'>"
                                echo 
    number_format((mysql_result($result,$ctr,"inputqty")),2);   
                                
    //echo mysql_result ($result,$ctr,"inputqty"); 
                                 
                                
    echo "</td>"
                                echo 
    "<td class='tdclass'>"
                                echo 
    number_format((mysql_result($result,$ctr,"outputqty")),2);   
                               
    // echo mysql_result ($result,$ctr,"outputqty"); 
                                 
                                
    echo "</td>"
                              }  
                                 
                            echo 
    "<td class='tdclass'><strong>"
                            
    printf ("%01.2f"$PLT_yield); 
                            echo 
    "%</strong></td>"
                            
    /*echo "<td>&nbsp;"; 
                            echo "</td>";*/ 
                            
    echo "</tr>"
                             
                         
                            } 
                        } 
                    } 
                } 
                
    $Batch_yield = ($sol_output $buff_input) * 100
                echo 
    "<tr>"
                echo 
    "<td style='text-align: right; font-weight: bold; font-family: Arial;' class = 'tdclass' colspan='"
                
    $loopctr = ($total_row 2) + 2
                echo 
    "$loopctr'>Batch Yield:&nbsp;&nbsp;$Batch_yield%</td>"
                echo 
    "</tr>"
                echo 
    "</table>";                
                
    //echo "<div style='text-align: right; font-family: arial narrow; font-size: 12px;'><strong>Batch Yield:&nbsp;&nbsp;$Batch_yield%</strong></div>"; 
           
    }  

    [
    IMG]https://www.go4expert.com/images/misc/progress.gif[/IMG]
     
    Last edited by a moderator: Jan 21, 2017
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,367
    Likes Received:
    386
    Trophy Points:
    83
    No I don't need to see the code and you can safely test the numbers and if they are non-zero then only perform the operation.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice