MySql and PHP sum 2 fields

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    MySql and PHP sum 2 fields

    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:

    PHP Code:
    function mksize($bytes)
    {
        if (
    $bytes 1000 1024)
                        return 
    number_format($bytes 10242) . " kB";
        elseif (
    $bytes 1000 1048576)
                        return 
    number_format($bytes 10485762) . " MB";
        elseif (
    $bytes 1000 1073741824)
                        return 
    number_format($bytes 10737418242) . " Gb";
        else
                        return 
    number_format($bytes 10995116277762) . " 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?

    #2
    Try this:
    PHP Code:
    $tt  mysql_fetch_array(mysql_query("SELECT SUM(uploaded), SUM(downloaded) FROM tbl_name WHERE id = ".$USER['id']));
    echo 
    mksize($tt[0] + $tt[1]); 

    Comment


      #3
      Thank you! This is Working!

      Comment

      Working...
      X