MySql and PHP sum 2 fields

Started on the old forum in September 2013 · view the original in the archive · old links redirect here

2 replies · 379 views · started 13 years ago · 1 min read

2 participants
cedry2k Junior · 15 posts #1

Hi,

I have the following problem: I have two fields in a mysql table, uploaded and downloaded and I want to sum them and show the result in a another way, here is my code:

function mksize($bytes)
{
	if ($bytes < 1000 * 1024)
					return number_format($bytes / 1024, 2) . " kB";
	elseif ($bytes < 1000 * 1048576)
					return number_format($bytes / 1048576, 2) . " MB";
	elseif ($bytes < 1000 * 1073741824)
					return number_format($bytes / 1073741824, 2) . " Gb";
	else
					return number_format($bytes / 1099511627776, 2) . " TB";
}

$tt = mysql_query("SELECT * FROM tbl_name WHERE id = ".$USER['id']) or die(mysql_error());
while($tt_row = mysql_fetch_assoc($tt)){
$totaludt = $tt_row['uploaded'] + $tt_row['downloaded'];
$totalud = mksize($totaludt);
echo $totalud;

This will echo the value of uploaded field but if I echo $tt_row['uploaded'] + $tt_row['downloaded']; will echo sum of the two fields.

what I am doing wrong?

do{ beer++ }while(sober); ===//\==\//=== while($lifes_a_bitch == true) { live(); } die;

something else Fellow · 2,296 posts #2

Try this:

$tt  = mysql_fetch_array(mysql_query("SELECT SUM(uploaded), SUM(downloaded) FROM tbl_name WHERE id = ".$USER['id']));
echo mksize($tt[0] + $tt[1]);
cedry2k Junior · 15 posts #3

Thank you! This is Working!

do{ beer++ }while(sober); ===//\==\//=== while($lifes_a_bitch == true) { live(); } die;

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close