jokerfool Posted January 6, 2009 Share Posted January 6, 2009 Getting a division by zero in this line, what is wrong with it, what does division by zero refer too and how can I fix it please? $percent = round((($rankp-$old)/($max-$old))*100, 1); Thank you Quote Link to comment https://forums.phpfreaks.com/topic/139620-warning-division-by-zero/ Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 You cannot divide by zero...basic math. What is held in $max and $old...? When they are subtracted it equals 0, thus your error... Quote Link to comment https://forums.phpfreaks.com/topic/139620-warning-division-by-zero/#findComment-730465 Share on other sites More sharing options...
jokerfool Posted January 6, 2009 Author Share Posted January 6, 2009 <?php session_start(); include "includes/db_connect.php"; include "includes/functions.php"; logincheck(); $username=$_SESSION['username']; $fetch=mysql_fetch_object(mysql_query("SELECT * FROM users WHERE username='$username'")); $currank=$fetch->rank; $rankp = $fetch->rankpoints; if ($currank == "Tramp"){ $max = '350'; $old="1"; }elseif ($currank == "Theif"){ $max = '750'; $old="350"; }elseif ($currank == "Hooligan"){ $max = '2320'; $old="750"; }elseif ($currank == "Criminal"){ $max = '4000'; $old="2320"; }elseif ($currank == "Gangster"){ $max = '6000'; $old="4000"; }elseif ($currank == "Boss"){ $max = '7200'; $old="6000"; }elseif ($currank == "Supreme Boss"){ $max = '11500'; $old="7200"; } elseif ($currank == "Don"){ $max = '17200'; $old="11500"; } elseif ($currank == "Legendary Don"){ $max = '150000'; $old="17200"; } elseif ($currank == "Mod"){ $max = '17048'; $old="1"; } elseif ($currank == "Admin"){ $max = '17048'; $old="1"; } elseif ($currank == "Owner"){ $max = '17048'; $old="1"; } $percent = round((($rankp-$old)/($max-$old))*100, 1); $check = mysql_query("SELECT * FROM `inbox` WHERE `read`='0' AND `to`='$username'"); $inbox=mysql_num_rows($check); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title></title> <link href="includes/marquee.css" rel="stylesheet" type="text/css" /> <script src="includes/marquee.js" type="text/javascript"></script> </head> <body dir="ltr"> <div id="marquee"> <div><a href="inbox.php" target="middle">Rank: <?php echo "$fetch->rank"; ?> | Money: <?php echo "£".makecomma($fetch->money).""; ?> | Location: <?php echo "$fetch->location"; ?> | Health: <?php echo "$fetch->health"; ?>% | Points: <?php echo "$fetch->points"; ?> | Crew: <?php if ($fetch->crew == "0"){ echo "None"; }else{ echo "$fetch->crew"; } ?> | Bullets: <?php echo "".makecomma($fetch->bullets).""; ?> | Inbox: <?php if ($inbox > 0 ) { echo "$inbox New message(s)."; } else {echo "No new messages."; } ?></a></div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/139620-warning-division-by-zero/#findComment-730470 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 Do this: if ($max-$old == 0) { die("Error these two equal 0 Max: " . $max . " Old: " . $old . " Equals: " . ($max - $old)); }else { $percent = round((($rankp-$old)/($max-$old))*100, 1); } And see what displays/report back. I bet that $max and $old are not hitting any of the if statements, thus they do not have a value which in return would give the division by zero error. If you can have a default value for $max and $old I would set that in your script before the if's or set it as an else at the end of the if's so this error does not happen if the $currank is not apart of any of the if's. Quote Link to comment https://forums.phpfreaks.com/topic/139620-warning-division-by-zero/#findComment-730471 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.