• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[SQL] Suming values

AGS

DeathSouls Owner
Joined
Oct 29, 2007
Messages
400
Reaction score
10
Location
Mexico
Another weird request for help

I need a php script, that sums the values from certain keys from player_storage.

For example, you do a query like this:
Code:
SELECT * FROM `player_storage` WHERE `key` = 366
Then you will get something like this:
Code:
player_id	key	value
3		366	757
67		366	17534
68		366	3737

So, what I need, is a php script that just returns 22028, I mean, the sum of the values.


Also, to avoid creating another post, does anyone knows how to hide decimals?
Like, turning 0.888888888889 to 0.888, 0.93023255814 to 0.930, 0.653846153846 to 0.653, etc
 
Last edited:
I - Sum:

PHP:
$array = array ( '757' , '17534' , '3737' );
$sum = 0;

foreach ( $array as $value )
{
	$sum += $value;
}

echo $sum;
That would output 22028.
(If you have troubles converting the array to your SQL results, please let me know so).




II - Decimals:

PHP:
echo round ( 0.888888888889 , 3 );
That would output 0.888.
 
Thanks!
Finally someone answered.

But how do I sum the values from the database?
 
Hehe, simply run a while loop and change the array line, like this:
PHP:
$result = mysql_query ( 'SELECT * FROM `player_storage` WHERE `key` = "366"' );

while ( $row = mysql_fetch_array ( $result ) )
{
	$array[] = $row['value'];
	$sum = 0;
	
	foreach ( $array as $value )
	{
	    $sum += $value;
	}  
}

echo $sum;
 
You can always make MySQLsum it for you:
SELECT SUM(value) FROM `player_storage` WHERE `key` = 366

This would return:
Code:
22028
 
Back
Top