Jump to content

Large numbers and PHP/MySQL


phpscriptcoder

Recommended Posts

I'm trying to fix someone's game script. It has some code which inserts a new row into the database, and 3 of the fields can be very large (in the quadrillions). But when the row is inserted, the 3 large fields are set to 0. The field types are BIGINT unsigned with length 21. Before being inserted into the database, the number is added to a variable which is often 0, if that matters.

 

Thanks for helping!

Link to comment
Share on other sites

Here is some code.

 

			$Mining['metal']   = 0;
		$Mining['crystal'] = 0;
		$Mining['deuter']  = 0;

...

			if ($FleetStorage > 0) {
				$metal   = $TargetPlanet['metal'] / 2;
				$crystal = $TargetPlanet['crystal'] / 2;
				$deuter  = $TargetPlanet["deuterium"] / 2;
				if (($metal) > $FleetStorage / 3) {
					$Mining['metal']   = $FleetStorage / 3;
					$FleetStorage      = $FleetStorage - $Mining['metal'];
				} else {
					$Mining['metal']   = $metal;
					$FleetStorage      = $FleetStorage - $Mining['metal'];
				}

				if (($crystal) > $FleetStorage / 2) {
					$Mining['crystal'] = $FleetStorage / 2;
					$FleetStorage      = $FleetStorage - $Mining['crystal'];
				} else {
					$Mining['crystal'] = $crystal;
					$FleetStorage      = $FleetStorage - $Mining['crystal'];
				}

				if (($deuter) > $FleetStorage) {
					$Mining['deuter']  = $FleetStorage;
					$FleetStorage      = $FleetStorage - $Mining['deuter'];
				} else {
					$Mining['deuter']  = $deuter;
					$FleetStorage      = $FleetStorage - $Mining['deuter'];
				}
			}
...
		$Mining['metal']   = round($Mining['metal']);
		$Mining['crystal'] = round($Mining['crystal']);
		$Mining['deuter']  = round($Mining['deuter']);

...

		$Mining['metal']   = $Mining['metal']   + $FleetRow["fleet_resource_metal"];
		$Mining['crystal'] = $Mining['crystal'] + $FleetRow["fleet_resource_crystal"];
		$Mining['deuter']  = $Mining['deuter']  + $FleetRow["fleet_resource_deuterium"];

		$QryUpdateFleet  = "UPDATE {{table}} SET ";
		$QryUpdateFleet .= "`fleet_amount` = '". $FleetAmount ."', ";
		$QryUpdateFleet .= "`fleet_array` = '". $FleetArray ."', ";
		$QryUpdateFleet .= "`fleet_mess` = '1', ";
		$QryUpdateFleet .= "`fleet_resource_metal` = '". $Mining['metal'] ."', ";
		$QryUpdateFleet .= "`fleet_resource_crystal` = '". $Mining['crystal'] ."', ";
		$QryUpdateFleet .= "`fleet_resource_deuterium` = '". $Mining['deuter'] ."' ";
		$QryUpdateFleet .= "WHERE fleet_id = '". $FleetRow['fleet_id'] ."' ";
		$QryUpdateFleet .= "LIMIT 1 ;";
		doquery( $QryUpdateFleet , 'fleets');

Link to comment
Share on other sites

UPDATE {{table}} SET `fleet_amount` = '290401619', `fleet_array` = '203,290401619;', `fleet_mess` = '1', `fleet_resource_metal` = '2.42001349167E+15', `fleet_resource_crystal` = '2.42001349167E+15', `fleet_resource_deuterium` = '2.42001349167E+15' WHERE fleet_id = '6772' LIMIT 1 ; 

 

I'm not sure if that's the problem, because it works for smaller numbers.

Link to comment
Share on other sites

The problem is that you're quoting the numbers which makes them into strings. Putting a string into a numeric field will cause a zero to be entered.

 

Nope. Unless there is some SQL compatibility mode that enforces that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.