Warning: Division by zero in

Hello guys, I was wondering what this error exactly means and I’d like to fix it but I don’t know how :frowning:

This is the exact error message: “Warning: Division by zero in /var/customers/webs/ni123045_4/players.php on line 85”

Here is my players.php code:

[php]<?php
@include_once(‘set.php’);
@include_once(‘steamauth/steamauth.php’);
@include_once “langdoc.php”;
$lang = $_COOKIE[“lang”];

$gamenum = fetchinfo(“value”,“info”,“name”,“current_game”);
if(!isset($_SESSION[“steamid”])) $admin = 0;
else $admin = fetchinfo(“admin”,“users”,“steamid”,$_SESSION[“steamid”]);
$ls=0;
$rs = mysql_query(“SELECT * FROM game".$gamenum." GROUP BY userid ORDER BY id DESC”);
//$rs = mysql_query(“SELECT * FROM game39 GROUP BY userid ORDER BY id DESC”);
//$rs23 = mysql_query(“SELECT * FROM currentplayers GROUP BY value ORDER BY value DESC”);
echo mysql_error();

?>

<?php if(mysql_num_rows($rs) == 0): //if there's no game going delete new players mysql_query("DELETE FROM `currentplayers`"); ?> <?php else: ?>
<?php
//var_dump($xda['uid']);
while($row=mysql_fetch_array($rs)):
	$avatar = $row["avatar"];
	$uid = $row['userid'];
	$rowcurrent = mysql_query("SELECT * FROM `currentplayers` WHERE `uid` = '$uid'");
	$xda = mysql_fetch_array($rowcurrent);

	$username = mysql_real_escape_string(fetchinfo("name", "users", "steamid", $uid));
	$steamprofile2x = fetchinfo("steamprofile", "users", "steamid", $uid);
	$steamid = fetchinfo("steamid", "users", "steamid", $uid);
	$rs2 = mysql_query("SELECT SUM(value) AS value FROM `game".$gamenum."` WHERE `userid`='$uid'");
	//$rs2 = mysql_query("SELECT SUM(value) AS value FROM `game39` WHERE `userid`='$uid'");
	$row2 = mysql_fetch_assoc($rs2);
	//var_dump($xda['uid']);
	//var_dump($xda['uid']);
	$value =$row2['value'];	
	$value2 =$row2["value"];
	$value2 = str_replace(".","",$value2);
	mysql_query("INSERT INTO currentplayers (id, uid, value, username, steamprofile2x, steamid, avatar, byvalue) VALUES ('', '$uid', '$value', '$username', '$steamprofile2x', '$steamid', '$avatar', '$value2') " );
	//var_dump($xda['uid']);
	switch ($xda) {
		case (is_null($xda['value'])):
		mysql_query("INSERT INTO currentplayers (id, uid, value, username, steamprofile2x, steamid, avatar, byvalue) VALUES ('', '$uid', '$value', '$username', '$steamprofile2x', '$steamid', '$avatar', '$value2') " );
			break;
		case ($xda['uid'] == $row['userid']):
			mysql_query("UPDATE `currentplayers` SET `value`='".$value."', `username`='".$username."', `steamprofile2x`='".$steamprofile2x."', `byvalue`='".$value2."' , `steamid`='".$steamid."', `avatar`='".$avatar."' WHERE `uid`='".$row['userid']."' ");
			break;
		case(mysql_num_rows(mysql_query('SELECT * FROM `currentplayers` WHERE `uid`="'.$uid.'" AND `value`="'.$value.'"'))==0):
		mysql_query("INSERT INTO currentplayers (id, uid, value, username, steamprofile2x, steamid, avatar, byvalue) VALUES ('', '$uid', '$value', '$username', '$steamprofile2x', '$steamid', '$avatar', '$value2') " );
		break;

		case($xda['value'] != $value):
		mysql_query("UPDATE `currentplayers` SET `value`='$value', `username`='$username',`byvalue`='$value2', `steamprofile2x`='$steamprofile2x', `steamid`='$steamid', `avatar`='$avatar', `value`='$value' WHERE `value`='$value ");
		break;
		default:
			mysql_query("INSERT INTO currentplayers (id, uid, value, username, steamprofile2x, steamid, avatar, byvalue) VALUES ('', '$uid', '$value', '$username', '$steamprofile2x', '$steamid', '$avatar', '$value2') " );
			break;

		 }

?>

<?php endwhile; endif; $rs23 = mysql_query("SELECT * FROM currentplayers ORDER BY value DESC "); if(mysql_num_rows($rs) == 0): else: while($rsx=mysql_fetch_array($rs23)):?>
	<?php

$thisgame = fetchinfo(“value”,“info”,“name”,“current_game”);
$bank = fetchinfo(“cost”,“games”,“id”,$thisgame);

$thisuserid=$rsx[‘uid’];

$result = mysql_query(“SELECT SUM(value) AS value FROM game.$thisgame WHERE userid=’$thisuserid’”);
$getrowval = mysql_fetch_assoc($result);
$generatechance=round($getrowval[“value”]*100/$bank,1);
$howmanyitems=mysql_num_rows(mysql_query(‘SELECT * FROM game'.$thisgame.' WHERE userid="’.$thisuserid.’"’));

$profilele=‘http://steamcommunity.com/profiles/’.$thisuserid;
?>
<?php $val = $rsx['value']; ?>

	<div style="padding:10px;">
	<div style="float:left;">
	<img src="<?php echo $rsx['avatar']; ?>" style="max-width:70px;padding:0px;margin:10px;-webkit-border-radius: 70px;

-moz-border-radius: 70px;
border-radius: 70px;"/>


		<h4 style="margin-top:7px;margin-bottom:5px"><a href="<?php echo $profilele; ?>" target="_blank"><?php echo htmlspecialchars($rsx['username']); ?></a></h4>
		<span style="font-size: 12px;color: #818181;font-weight:normal;"><?php echo $generatechance; ?>% chance with <?php echo $howmanyitems; ?> item<?php if($howmanyitems>1) echo 's'; ?> valued at $<?php echo round($val, 2); ?></span>
		</span>
<?php //echo''; ?>
<div id="progressbar" style="margin-top:3px;margin-left:80px;max-width:78%;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;">
  <div style="width:<?php echo $generatechance; ?>%"></div>
</div>


	</div>
	</div><br/>
<?php endwhile; ?> <?php endif; ?>

[/php]

I appreciate any kind of help :slight_smile:

Hello I recently got this PHP Error:

Warning: Division by zero in /var/customers/webs/ni123045_4/players.php on line 85

I don’t have a clue about it and I don’t know what’s wrong.

Here’s my code (players.php):

[php]<?php
@include_once(‘set.php’);
@include_once(‘steamauth/steamauth.php’);
@include_once “langdoc.php”;
$lang = $_COOKIE[“lang”];

$gamenum = fetchinfo(“value”,“info”,“name”,“current_game”);
if(!isset($_SESSION[“steamid”])) $admin = 0;
else $admin = fetchinfo(“admin”,“users”,“steamid”,$_SESSION[“steamid”]);
$ls=0;
$rs = mysql_query(“SELECT * FROM game".$gamenum." GROUP BY userid ORDER BY id DESC”);
//$rs = mysql_query(“SELECT * FROM game39 GROUP BY userid ORDER BY id DESC”);
//$rs23 = mysql_query(“SELECT * FROM currentplayers GROUP BY value ORDER BY value DESC”);
echo mysql_error();

?>

<?php if(mysql_num_rows($rs) == 0): //if there's no game going delete new players mysql_query("DELETE FROM `currentplayers`"); ?> <?php else: ?>
<?php
//var_dump($xda['uid']);
while($row=mysql_fetch_array($rs)):
	$avatar = $row["avatar"];
	$uid = $row['userid'];
	$rowcurrent = mysql_query("SELECT * FROM `currentplayers` WHERE `uid` = '$uid'");
	$xda = mysql_fetch_array($rowcurrent);

	$username = mysql_real_escape_string(fetchinfo("name", "users", "steamid", $uid));
	$steamprofile2x = fetchinfo("steamprofile", "users", "steamid", $uid);
	$steamid = fetchinfo("steamid", "users", "steamid", $uid);
	$rs2 = mysql_query("SELECT SUM(value) AS value FROM `game".$gamenum."` WHERE `userid`='$uid'");
	//$rs2 = mysql_query("SELECT SUM(value) AS value FROM `game39` WHERE `userid`='$uid'");
	$row2 = mysql_fetch_assoc($rs2);
	//var_dump($xda['uid']);
	//var_dump($xda['uid']);
	$value =$row2['value'];	
	$value2 =$row2["value"];
	$value2 = str_replace(".","",$value2);
	mysql_query("INSERT INTO currentplayers (id, uid, value, username, steamprofile2x, steamid, avatar, byvalue) VALUES ('', '$uid', '$value', '$username', '$steamprofile2x', '$steamid', '$avatar', '$value2') " );
	//var_dump($xda['uid']);
	switch ($xda) {
		case (is_null($xda['value'])):
		mysql_query("INSERT INTO currentplayers (id, uid, value, username, steamprofile2x, steamid, avatar, byvalue) VALUES ('', '$uid', '$value', '$username', '$steamprofile2x', '$steamid', '$avatar', '$value2') " );
			break;
		case ($xda['uid'] == $row['userid']):
			mysql_query("UPDATE `currentplayers` SET `value`='".$value."', `username`='".$username."', `steamprofile2x`='".$steamprofile2x."', `byvalue`='".$value2."' , `steamid`='".$steamid."', `avatar`='".$avatar."' WHERE `uid`='".$row['userid']."' ");
			break;
		case(mysql_num_rows(mysql_query('SELECT * FROM `currentplayers` WHERE `uid`="'.$uid.'" AND `value`="'.$value.'"'))==0):
		mysql_query("INSERT INTO currentplayers (id, uid, value, username, steamprofile2x, steamid, avatar, byvalue) VALUES ('', '$uid', '$value', '$username', '$steamprofile2x', '$steamid', '$avatar', '$value2') " );
		break;

		case($xda['value'] != $value):
		mysql_query("UPDATE `currentplayers` SET `value`='$value', `username`='$username',`byvalue`='$value2', `steamprofile2x`='$steamprofile2x', `steamid`='$steamid', `avatar`='$avatar', `value`='$value' WHERE `value`='$value ");
		break;
		default:
			mysql_query("INSERT INTO currentplayers (id, uid, value, username, steamprofile2x, steamid, avatar, byvalue) VALUES ('', '$uid', '$value', '$username', '$steamprofile2x', '$steamid', '$avatar', '$value2') " );
			break;

		 }

?>

<?php endwhile; endif; $rs23 = mysql_query("SELECT * FROM currentplayers ORDER BY value DESC "); if(mysql_num_rows($rs) == 0): else: while($rsx=mysql_fetch_array($rs23)):?>
	<?php

$thisgame = fetchinfo(“value”,“info”,“name”,“current_game”);
$bank = fetchinfo(“cost”,“games”,“id”,$thisgame);

$thisuserid=$rsx[‘uid’];

$result = mysql_query(“SELECT SUM(value) AS value FROM game.$thisgame WHERE userid=’$thisuserid’”);
$getrowval = mysql_fetch_assoc($result);
$generatechance=round($getrowval[“value”]*100/$bank,1);
$howmanyitems=mysql_num_rows(mysql_query(‘SELECT * FROM game'.$thisgame.' WHERE userid="’.$thisuserid.’"’));

$profilele=‘http://steamcommunity.com/profiles/’.$thisuserid;
?>
<?php $val = $rsx['value']; ?>

	<div style="padding:10px;">
	<div style="float:left;">
	<img src="<?php echo $rsx['avatar']; ?>" style="max-width:70px;padding:0px;margin:10px;-webkit-border-radius: 70px;

-moz-border-radius: 70px;
border-radius: 70px;"/>


		<h4 style="margin-top:7px;margin-bottom:5px"><a href="<?php echo $profilele; ?>" target="_blank"><?php echo htmlspecialchars($rsx['username']); ?></a></h4>
		<span style="font-size: 12px;color: #818181;font-weight:normal;"><?php echo $generatechance; ?>% chance with <?php echo $howmanyitems; ?> item<?php if($howmanyitems>1) echo 's'; ?> valued at $<?php echo round($val, 2); ?></span>
		</span>
<?php //echo''; ?>
<div id="progressbar" style="margin-top:3px;margin-left:80px;max-width:78%;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;">
  <div style="width:<?php echo $generatechance; ?>%"></div>
</div>


	</div>
	</div><br/>
<?php endwhile; ?> <?php endif; ?>

[/php]

I hope you can help me as this error sucks :confused:

Where do I start:

mysql_ functions are deprecated and should not be used at all.

You should be using prepared statements. You current state is completely open to SQL Injection, that even a non-malicous user would break you application when entering information not expected.

This:[php]
?>

<?php[/php] Is laced throughout your code and is pointless to close than open PHP. You should not be breaking in and out of PHP just because. Code separation makes this easier to deal with. What are you expecting these values to be? [php]round($getrowval["value"]*100/$bank,1);[/php] This is where the error is occurring, so either your formula is incorrect or your variables are not holding the proper values.

The entire code and the database layout are very, very broken. I’m actually surprised that the script gets as far as line 85.

[ul][li]The [tt]switch[/tt] statement doesn’t work like this. You seem to confuse it with the [tt]if[/tt] statement.[/li]
[li]The relational database model doesn’t work like this. Adding a new column whenever there’s a new game is very poor design, and running queries within fetch loops will kill the performance. You should learn basics concepts like normalization and joins before jumping to more complex tasks.
[/li][li]Your use of [tt]GROUP BY[/tt] makes no sense.[/li]
[li]Suppressing error messages with @ is a very bad idea, because you lose important information about all the bugs in your code.[/li]
[li]Your formatting is atrocious.[/li]
[li]All the PHPHTMLCSSJavaScript spaghetti code further decreases readability.[/li][/ul]

Maybe you should start from scratch after you’ve learned the basics of PHP and MySQL. It doesn’t really make sense to keep working on a script when almost every line is wrong.

Sponsor our Newsletter | Privacy Policy | Terms of Service