Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts

    Warning: Division by zero

    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 Code:
    $yield = ($c_output $f_input) * 100
    and this is my code in line 220
    PHP Code:
    $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
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,432
    Thanks
    17
    Thanked 273 Times in 273 Posts
    $yield = $c_output / ($f_input * 100);

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DrDOS View Post
    $yield = $c_output / ($f_input * 100);
    Thank you for the suggested code, I tried it but nothing happen...the warning was still there

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,432
    Thanks
    17
    Thanked 273 Times in 273 Posts
    Maybe try echoing all three variable values right after that code to see what results. The problem might be with the database.

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by newphpcoder View Post
    Warning: Division by zero in C:\Documents and Settings\LT\report.php on line 122

    I have this code in line 22:
    Is that a typo or are you looking at the wrong line?

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    Is that a typo or are you looking at the wrong line?
    here is my full code:
    PHP Code:
    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>";
           


  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Multiplying anything by 0 is still 0, so $var / ($var2 * 100) won't work if $var2 is 0. That problem will still remain.
    The problem is simple, solution is also somewhat simple. Looking at this, you have built your $f_input and $buff_input to conditionally provide data from a query. If there is a failure in the query or the query provides no results, than these variables result in 0 by default.
    I don't know how this is all related, nor do I want to go through that much to determine how. Check that your $f_input and $buff_input are not zero before trying to divide by them. Alternatively, you could initialize these as 1 instead of 0. Not sure if its possible for your application to have > 100% or not (if $c_output > 1 and $f_input is == 0, than the result would be $c_output * 100).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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