-4

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?

http://gyazo.com/26b3309c8e72d89ccc4cd32f4cdae7cd

Kermit
  • 33,827
  • 13
  • 85
  • 121
user1761494
  • 127
  • 2
  • 11
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Jan 27 '13 at 19:06
  • 3
    And what do you expect when dividing by zero? – dev-null-dweller Jan 27 '13 at 19:08
  • 1
    As the warning says, it's happening because you can't divide by zero. – Walfie Jan 27 '13 at 19:08
  • 1
    If I divide by zero it tells me that there's a division by zero... is this a real question? – Francisco Presencia Jan 27 '13 at 19:09

2 Answers2

1

It's happening because a division by zero is undefined. If KC and DC are zero, your equation is 0/0, which is a division by zero.

To fix, you simply need to ensure that your divisor (the number you are dividing by) is not zero.

In the case of your example, if the player has never died, you will get a division by zero. Simply check if $row['deaths'] is zero and if so, display an appropriate symbol. In many cases, a division by zero is accepted to yield infinity.

slugonamission
  • 9,562
  • 1
  • 34
  • 41
1

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:

<?php
$result = mysql_query("SELECT * FROM high ORDER BY Runecraftlvl DESC LIMIT 20");
while($row = mysql_fetch_array($result))
  {
  echo ($row['deaths'] != 0) ? $row['kills'] / $row['deaths'] : '-';
  echo "<br />";
  }
?>

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?

Community
  • 1
  • 1
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
  • How do I order the value? so It displays from the highest to lowest? – user1761494 Jan 27 '13 at 19:20
  • I'm completely sure there's some way to do it strictly in mysql, but unfortunately I don't know how to. Right now you are ordering by Runecraftlvl. I could say how to order the ones you are displaying, but this doesn't imply that they are the 20 highest ones since you are only fetching the 20 top level ones (the 21st, for example, could have a higher `$row['kills'] / $row['deaths']` radious. So I suggest you try to do it with mysql directly and if you have any problem you ask another question (: – Francisco Presencia Jan 27 '13 at 19:28
  • I edited it to add some info based on your comments. If my answer answered your original question, you can mark it as "accepted" under the votes (; – Francisco Presencia Jan 27 '13 at 19:33