I have this script:
<?php
$result = mysql_query("SELECT * FROM high ORDER BY KC DESC LIMIT 20");
while($row = mysql_fetch_array($result))
{
echo $row['KC'];
echo "<br />";
}
?>
And this
<?php
$result = mysql_query("SELECT * FROM high ORDER BY DC DESC LIMIT 20");
while($row = mysql_fetch_array($result))
{
echo $row['DC'];
echo "<br />";
}
?>
I want to divine DC and KC and want it to show the value after dividing.
For example DC = 80 and KC = 30
The system will divine KC to DC ( 80 / 30 = 2.666666666666667 )
I tried doing this:
<?php
$result = mysql_query("SELECT * FROM high ORDER BY Runecraftlvl DESC LIMIT 20");
while($row = mysql_fetch_array($result))
{
echo $row['kills'] / $row['deaths'];
echo "<br />";
}
?>
It works just fine, but if both variables KC and DC are 0, it gives me this error:
Warning: Division by zero in /home/justxpp1/public_html/high/highscores.php on line 86
Why is that happening?
While this is a too basic question, here’s a possible solution. You only have to check the second variable, the first one is okay even if it’s 0:
PD, it uses the ternary operator.
I found a post where it explains how to do what you requested. For your particular request, this is also important: How to avoid the “divide by zero” error in SQL?