Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2002
    Location
    Ohio
    Posts
    3,139

    Question Adding results from MySQL

    I'm working on a script that deals with money, and I cant for the life of me figure out how to add all the results from a specific column together to get the "total net worth" of all the money stored in a column.

    I'm sure it is something totally simple... The code below returns an incorrect number.

    PHP Code:
    $result sql_query("select balance, pending from user_accts"$dbi);
    while(list(
    $balance$pending) = sql_fetch_row($result$dbi)) {
            
    $totalbal = ++$balance;
            
    $totalpen = ++$pending;
            
    $gross $totalpen $totalbal;

    Thanks in advance
    Glioblastoma Multiforme (GBM) Brain Cancer Awareness. May is Brain Cancer awareness month. Gray Matters!
    Incurable, 6-18 months prognosis, survivors longer than 3 years less than 1% chance.
    Don't like what I say? Ignore me.

  2. #2
    Hi,
    Try this:
    $result = mysql_query ("SELECT SUM(balance) as total FROM user_accts");
    list ($total) = mysql_fetch_row($result);
    echo $total;
    web:http://www.allabouthosting.co.uk
    email: [email protected]

  3. #3
    Join Date
    Jul 2002
    Location
    Israel
    Posts
    351
    Uhm.

    PHP Code:
    $ret mysql_query("SELECT SUM(pending) as totalpen,SUM(balance) AS totalbal FROM user_accts",$dbi);
    $r mysql_fetch_assoc($ret);
    printf("Balance: %d<br />\n, Pending: %d\n"$r['totalbal'],$r['totalpen']); 
    C#/C++/TCL/Python/PHP developer.
    mark at mark org il

  4. #4
    Join Date
    Jan 2002
    Location
    Ohio
    Posts
    3,139
    Thanks everyone... It's working now after I made myself look like a fool over at Devshed .

    Heres the code I settled with. Ideally it is not a perfect solution, but since the code is for a project I'm probably not going to distribute.

    PHP Code:
            $result sql_query("select sum(balance), sum(pending) from user_accts"$dbi);
            while(list(
    $sumbal$sumpend) = sql_fetch_row($result$dbi)) {
                    
    $gross $sumbal $sumpend;
                    
    $gross number_format($gross2'.'',');
                    
    $sumbal number_format($sumbal2'.'',');
                    
    $sumpend number_format($sumpend2'.'',');
                    
    $funds "We have $$sumbal in current balances.<br>We have $$sumpend in pending funds.";
            } 
    Glioblastoma Multiforme (GBM) Brain Cancer Awareness. May is Brain Cancer awareness month. Gray Matters!
    Incurable, 6-18 months prognosis, survivors longer than 3 years less than 1% chance.
    Don't like what I say? Ignore me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •