[PHP] Sum a column of values from a MySQL query

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default [PHP] Sum a column of values from a MySQL query

    I am trying to sum a query of values from a MySQL table. The code I am
    using is:

    ---BEGIN CODE #1--------------
    $sql_2 = "SELECT SUM(partpaidamount) as partpaid
    FROM $tb_name
    WHERE invoiceid = \"$invoiceid\"
    ";

    $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error());

    while ($row = mysql_fetch_array($result_2)) {
    $invoicepartpaid = $row['partpaid'];
    }
    ---END CODE #2----------------

    1) The code returns the sum of a partially paid invoice. The individual
    invoice is 'partpaid'. WORKS...NO PROBLEM
    2) The while() then return a list of partially paid invoices which is
    $invoicepartpaid. WORKS..NO PROBLEM
    3) I then want to add the list of partially paid invoices ($invoicepartpaid)
    together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER.

    If anyone can help I would greatly appreciate it.

    Ben C. Guest

  2. Similar Questions and Discussions

    1. MySQL Query Cache Not Working: MySQL 5 / Windows XP
      Please excuse this post if you've already read it on mailing.database.mysql - i just discovered these other groups. future posts will be...
    2. php/mysql query insert values into enters the records in reverse order
      I'm loading tab delimited lines from a txt file using php and running a query for each line to enter the data into a mysql database. However the...
    3. multiple menu/list values inserted into one mysql column
      Simple problem here for the pros I'm sure. I have constructed a "rockshow" database in mysql for local music artists/bands and I made an input form...
    4. Sum a column of values from a MySQL query
      I am trying to sum a query of values from a MySQL table. The code I am using is: ---BEGIN CODE #1-------------- $sql_2 = "SELECT...
    5. how to Add different controls(textBox,DropDownList or some ) in the same column,based upon the value in the previous column (Say second Colum which contain dropdown with some values) ?
      I am new to ASP.NET. I am facing problem with datagrid. I will explain prob now. In the datagrid the first column is name in the second...
  3. #2

    Default RE: [PHP] Sum a column of values from a MySQL query

    I think you're a big confused here. Your query will only return one row,
    because you're using sum(). $invoicepartpaid will be your total for the
    invoiceid specified.

    -----Original Message-----
    From: Ben C. [mailto:benc@cox.net]
    Sent: Monday, August 04, 2003 11:42 PM
    To: [email]php-general@lists.php.net[/email]
    Subject: [PHP] Sum a column of values from a MySQL query

    I am trying to sum a query of values from a MySQL table. The code I am
    using is:

    ---BEGIN CODE #1--------------
    $sql_2 = "SELECT SUM(partpaidamount) as partpaid
    FROM $tb_name
    WHERE invoiceid = \"$invoiceid\"
    ";

    $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error());

    while ($row = mysql_fetch_array($result_2)) {
    $invoicepartpaid = $row['partpaid'];
    }
    ---END CODE #2----------------




    Giz Guest

  4. #3

    Default RE: [PHP] Sum a column of values from a MySQL query

    Yes, I know. However, the while() loop should generate all the invoice in a
    list.

    -----Original Message-----
    From: Giz [mailto:gizmo@gizmola.com]
    Sent: Tuesday, August 05, 2003 12:03 AM
    To: 'Ben C.'; [email]php-general@lists.php.net[/email]
    Subject: RE: [PHP] Sum a column of values from a MySQL query


    I think you're a big confused here. Your query will only return one row,
    because you're using sum(). $invoicepartpaid will be your total for the
    invoiceid specified.

    -----Original Message-----
    From: Ben C. [mailto:benc@cox.net]
    Sent: Monday, August 04, 2003 11:42 PM
    To: php-general@lists.php.net
    Subject: [PHP] Sum a column of values from a MySQL query

    I am trying to sum a query of values from a MySQL table. The code I am
    using is:

    ---BEGIN CODE #1--------------
    $sql_2 = "SELECT SUM(partpaidamount) as partpaid
    FROM $tb_name
    WHERE invoiceid = \"$invoiceid\"
    ";

    $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error());

    while ($row = mysql_fetch_array($result_2)) {
    $invoicepartpaid = $row['partpaid'];
    }
    ---END CODE #2----------------





    --
    PHP General Mailing List (http://www.php.net/)
    To unsubscribe, visit: http://www.php.net/unsub.php

    Ben C. Guest

  5. #4

    Default Re: [PHP] Sum a column of values from a MySQL query

    On Tuesday 05 August 2003 15:43, Ben C. wrote:
    > Yes, I know. However, the while() loop should generate all the invoice in
    > a list.
    As has been pointed out the query only results in a single row. So how would
    the while-loop "generate all the invoice[s]"? It's only ever given a single
    row to play with.

    And has also been pointed out, could you state clearly:

    - what exactly you're trying to do
    - what your database schema is
    - what you tried that didn't work, and *how* it didn't work

    --
    Jason Wong -> Gremlins Associates -> [url]www.gremlins.biz[/url]
    Open Source Software Systems Integrators
    * Web Design & Hosting * Internet & Intranet Applications Development *
    ------------------------------------------
    Search the list archives before you post
    [url]http://marc.theaimsgroup.com/?l=php-general[/url]
    ------------------------------------------
    /*
    I appoint you ambassador to Fantasy Island!!!
    */

    Jason Wong Guest

  6. #5

    Default RE: [PHP] Sum a column of values from a MySQL query

    [snip]
    However, why not just SUM all the rows in the table in the query if you
    just
    want a total?

    $sql_2 = "SELECT SUM(partpaidamount) as partpaid FROM $tb_name";
    [/snip]

    That is what he is doing. The SELECT SUM will only return ONE row! I am
    not sure that the problem is being explained clearly though.
    Jay Blanchard Guest

  7. #6

    Default RE: [PHP] Sum a column of values from a MySQL query

    [snip]
    1) The code returns the sum of a partially paid invoice. The individual
    invoice is 'partpaid'. WORKS...NO PROBLEM
    2) The while() then return a list of partially paid invoices which is
    $invoicepartpaid. WORKS..NO PROBLEM
    3) I then want to add the list of partially paid invoices
    ($invoicepartpaid)
    together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER.
    [/snip]

    Ben...do you want to add together invoices where each ID is different?
    Is that right? If so I have the answer! You need to loop through the
    ID's which you are not doing....I have taken your code and added to
    it....(and made changes for clarity in the e-amil)

    //get invoice id's
    $sql_1 = "SELECT DISTINCT(invoiceid) as inid FROM $tb_name ";
    $result_1 = mysql_query($sql_1, $connection) or die(mysql_error());

    while($idinvoice = mysql_fetch_array($result_1)){
    // get partial paid for THIS invoice
    $sql_2 = "SELECT SUM(partpaidamount) as partpaid ";
    $sql_2 .= "FROM $tb_name ";
    $sql_2 .= "WHERE invoiceid = '" . $idinvoice['inid'] . "' ";
    $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error());
    //you will only be returning ONE row, so no need for while loop
    $row = mysql_fetch_array($result_2);
    $invoicepartpaid = $row['partpaid'];
    $total = $total + $invoicepartpaid;
    } // loop to NEXT invoice

    print(number_format($total, 2, '', ','). "\n");

    Does this make sense? You must loop through each ID and query for that
    ID's partial amount.
    Jay Blanchard Guest

  8. #7

    Default Re: [PHP] Sum a column of values from a MySQL query

    Sorry, Jay, but that's a horrible method. You could just run one query with
    a GROUP BY clause to get the sum for each invoiceID, instead of running
    multiple queries like you've mentioned...

    $query = "SELECT invoiceid, SUM(partpaidamount) AS partpaid FROM $tb_name
    GROUP BY invoiceid";
    $rs = mysql_query($query) or die(mysql_error());
    while($r = mysql_fetch_assoc($rs))
    { echo "Invoice: {$r['invoiceid']}, SUM: {$r['partpaid']}"; }

    ---John Holmes...

    PS: Sorry for the top post, but I'm at work and outlook express sucks. :)

    ----- Original Message -----
    From: "Jay Blanchard" <jay.blanchard@niicommunications.com>
    To: "Ben C." <benc@cox.net>; <php-general@lists.php.net>
    Sent: Tuesday, August 05, 2003 8:29 AM
    Subject: RE: [PHP] Sum a column of values from a MySQL query


    [snip]
    1) The code returns the sum of a partially paid invoice. The individual
    invoice is 'partpaid'. WORKS...NO PROBLEM
    2) The while() then return a list of partially paid invoices which is
    $invoicepartpaid. WORKS..NO PROBLEM
    3) I then want to add the list of partially paid invoices
    ($invoicepartpaid)
    together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER.
    [/snip]

    Ben...do you want to add together invoices where each ID is different?
    Is that right? If so I have the answer! You need to loop through the
    ID's which you are not doing....I have taken your code and added to
    it....(and made changes for clarity in the e-amil)

    //get invoice id's
    $sql_1 = "SELECT DISTINCT(invoiceid) as inid FROM $tb_name ";
    $result_1 = mysql_query($sql_1, $connection) or die(mysql_error());

    while($idinvoice = mysql_fetch_array($result_1)){
    // get partial paid for THIS invoice
    $sql_2 = "SELECT SUM(partpaidamount) as partpaid ";
    $sql_2 .= "FROM $tb_name ";
    $sql_2 .= "WHERE invoiceid = '" . $idinvoice['inid'] . "' ";
    $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error());
    //you will only be returning ONE row, so no need for while loop
    $row = mysql_fetch_array($result_2);
    $invoicepartpaid = $row['partpaid'];
    $total = $total + $invoicepartpaid;
    } // loop to NEXT invoice

    print(number_format($total, 2, '', ','). "\n");

    Does this make sense? You must loop through each ID and query for that
    ID's partial amount.

    --
    PHP General Mailing List (http://www.php.net/)
    To unsubscribe, visit: http://www.php.net/unsub.php

    Cpt John W. Holmes Guest

  9. #8

    Default RE: [PHP] Sum a column of values from a MySQL query

    [snip]
    Sorry, Jay, but that's a horrible method. You could just run one query
    with
    a GROUP BY clause to get the sum for each invoiceID, instead of running
    multiple queries like you've mentioned...

    $query = "SELECT invoiceid, SUM(partpaidamount) AS partpaid FROM
    $tb_name
    GROUP BY invoiceid";
    $rs = mysql_query($query) or die(mysql_error());
    while($r = mysql_fetch_assoc($rs))
    { echo "Invoice: {$r['invoiceid']}, SUM: {$r['partpaid']}"; }

    ---John Holmes...

    PS: Sorry for the top post, but I'm at work and outlook express sucks.
    :)
    [/snip]

    No problem bro'. I knew it was the long way around I was trying to
    understand if that was what Ben wanted. And I am so old school that
    almost always avoid ternary! :) And I feel for you....Express...blech!
    Jay Blanchard Guest

  10. #9

    Default Re: [PHP] Sum a column of values from a MySQL query


    From bitter experience I'll suggest that it's a REALLY GOOD idea to store
    invoice totals, subtotals, etc. in either the invoice header record. If not
    rounding errors, or some small change in logic, can come back and bite you
    - accountants get *really* upset when that 3000.00 invoice becomes 3000.01
    or 2999.99.

    Know what else? THEY'LL NEVER LET YOU FORGET.

    Don't call it denormalization - call it history.

    Cheers - Miles


    At 03:53 PM 8/5/2003 +0800, Jason Wong wrote:
    >On Tuesday 05 August 2003 15:43, Ben C. wrote:
    > > Yes, I know. However, the while() loop should generate all the invoice in
    > > a list.
    >
    >As has been pointed out the query only results in a single row. So how would
    >the while-loop "generate all the invoice[s]"? It's only ever given a single
    >row to play with.
    >
    >And has also been pointed out, could you state clearly:
    >
    >- what exactly you're trying to do
    >- what your database schema is
    >- what you tried that didn't work, and *how* it didn't work
    >
    >--
    >Jason Wong -> Gremlins Associates -> [url]www.gremlins.biz[/url]
    >Open Source Software Systems Integrators
    >* Web Design & Hosting * Internet & Intranet Applications Development *
    >------------------------------------------
    >Search the list archives before you post
    >[url]http://marc.theaimsgroup.com/?l=php-general[/url]
    >------------------------------------------
    >/*
    >I appoint you ambassador to Fantasy Island!!!
    >*/
    >
    >
    >--
    >PHP General Mailing List ([url]http://www.php.net/[/url])
    >To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    Miles Thompson Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139