Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: How to sum $row

  1. #1
    New Coder
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to sum $row

    Hi there,

    For example: I echo the following 2:

    $row['SUM(sales1)']
    $row['SUM(sales2)']

    How can I Sum and echo them?

    I tried $total = $row['SUM(sales1)'] + $row['SUM(sales2)'] ;
    echo $total

    but not working.

    Thanks.

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,671
    Thanks
    24
    Thanked 580 Times in 573 Posts
    why don’t you do this sum in SQL as well? (that brings up the question why there are 2 sales columns)

    otherwise, enable error reporting/display and check (e.g. with var_dump() or print_r()) what’s really inside $row.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,698
    Thanks
    93
    Thanked 4,947 Times in 4,908 Posts
    And you SHOULD give an alias to those SUM's in the SQL:

    SELECT ... SUM(sales1) AS totalSales1, SUM(sales2) AS totalSales2

    And then the PHP

    echo $row("totalSales1") + $row("totalSales2");

    should work.

    But in any case, when you don't show your actual and full code to us, it's difficult to do more than GUESS. Stop making us guess. Give us *DETAILS*.
    Be yourself. No one else is as qualified.

  4. #4
    New Coder
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    And you SHOULD give an alias to those SUM's in the SQL:

    SELECT ... SUM(sales1) AS totalSales1, SUM(sales2) AS totalSales2

    And then the PHP

    echo $row("totalSales1") + $row("totalSales2");

    should work.

    But in any case, when you don't show your actual and full code to us, it's difficult to do more than GUESS. Stop making us guess. Give us *DETAILS*.
    I am sorry, basically I just want to compare between 2 columns, I have tried another methods as follow, however unable to echo the net result:

    $query = "SELECT
    SUM(bb_owe_comm) AS BBTOTAL
    FROM invoice
    WHERE invoice_id = '$search'";
    MINUS
    SELECT
    SUM (bb_bed) AS BBTOTAL
    FROM invoice
    WHERE package_deduction_inv = '$search'
    ";


    $data = mysqli_query ($dbc, $query);

    // Print out result
    while($row = mysqli_fetch_array($data)) {
    echo 'The amount is' .
    $row['BBTOTAL'];
    }

  5. #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,698
    Thanks
    93
    Thanked 4,947 Times in 4,908 Posts
    Where did you come up with the keyword MINUS???

    In any case, it certainly won't do what you want here!

    Try this:
    Code:
    SELECT 
        SUM( IF( invoice_id = '$search', 
                 bb_owe_comm, 
                 IF( package_deduction_inv = '$search', -bb_bed, 0 )
               )
        ) AS BBTOTAL
    FROM invoice
    An alternative that might be easier to understand:
    Code:
    SELECT 
        SUM( CASE '$search' 
             WHEN invoice_id THEN bb_owe_com 
             WHEN package_deduction_inv THEN - bb_bed
             ELSE 0 ) AS BBTOTAL
    FROM 
        invoice
    Last edited by Old Pedant; Oct 31st, 2014 at 01:44 AM.
    Be yourself. No one else is as qualified.

  6. #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,698
    Thanks
    93
    Thanked 4,947 Times in 4,908 Posts
    Hmmmm....that first one might be clearer done like this:
    Code:
    SELECT 
        SUM( IF( invoice_id = '$search', bb_owe_comm, 0 )
             -
             IF( package_deduction_inv = '$search', bb_bed, 0 )
        ) AS BBTOTAL
    FROM invoice
    Be yourself. No one else is as qualified.

  7. #7
    New Coder
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Hmmmm....that first one might be clearer done like this:
    Code:
    SELECT 
        SUM( IF( invoice_id = '$search', bb_owe_comm, 0 )
             -
             IF( package_deduction_inv = '$search', bb_bed, 0 )
        ) AS BBTOTAL
    FROM invoice
    Hi Old Pedant,

    Thanks. Both of your code work very well and easy to understand. Thanks for bringing us (the new coders) miracles.

    The MINUS appears in a lot of websites such as :
    Database Design - Union & minus
    MINUS and INTERSECT in MySQL | Gokhan Atil's Oracle Blog

  8. #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,698
    Thanks
    93
    Thanked 4,947 Times in 4,908 Posts
    Yes, but didn't you read the FIRST SENTENCE of that second URL???
    MySQL doesn’t support the INTERSECT and MINUS set operators. ...
    !! So why did you try to use it? But, in any case, it does *NOT* do what you think, *AT ALL*!!

    It treats each SELECT *as a unit*. So it would do the first SELECT, get only one ROW. Then it would do the second SELECT and get only one row. And then ONLY if the two rows MATCHED would the MINUS do anything. In this case, if they did, you would get no result at all. It is a *SET* operation, not an arithmetic operation.
    Be yourself. No one else is as qualified.


 

Posting Permissions

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