...

View Full Version : Warning: Division by zero



newphpcoder
11-22-2010, 03:30 AM
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:


$yield = ($c_output / $f_input) * 100;


and this is my code in line 220


$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

DrDOS
11-22-2010, 03:40 AM
$yield = $c_output / ($f_input * 100);

newphpcoder
11-22-2010, 03:55 AM
$yield = $c_output / ($f_input * 100);

Thank you for the suggested code, I tried it but nothing happen...the warning was still there

DrDOS
11-22-2010, 04:02 AM
Maybe try echoing all three variable values right after that code to see what results. The problem might be with the database.

MattF
11-22-2010, 04:08 AM
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?

newphpcoder
11-22-2010, 04:18 AM
Is that a typo or are you looking at the wrong line?

here is my full 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>";
}

Fou-Lu
11-22-2010, 08:36 PM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum