Ask a Question related to PHP Development, Design and Development.
-
Ben C. #1
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.
--------------------------------------------------------------------------
The content of this email message and any attachments are confidential and
may be legally privileged, intended solely for the addressee. If you are
not the intended recipient, be advised that any use, dissemination,
distribution, or copying of this e-mail is strictly prohibited. If you
receive this message in error, please notify the sender immediately by reply
email and destroy the message and its attachments.
Ben C. Guest
-
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... -
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... -
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... -
[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... -
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... -
Adam Alkins #2
Re: [PHP] Sum a column of values from a MySQL query
Quoting "Ben C." <benc@cox.net>:
Well, instead of doing $invoicepartpaid = $row['partpaid']; you could do> 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.
$invoicepartpaid += $row['partpaid']; which will just add $row['partpaid'] to
$invoicepartpaid, not replace it.
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";
--
Adam Alkins
[url]http://www.rasadam.com[/url]
Adam Alkins Guest
-
Jim Lucas #3
Re: Sum a column of values from a MySQL query
What are you expecting back from the sql statement?
And what are you getting?
From what I can tell, if you are getting all your amounts from the select
statement then you are already totalling them up inside the select
statements by using the sum() function call.
if you are wanting, or expecting, a row for each invoice, then you need to
remove the where clause and the sum() alias and then loop through the result
set and add partpaidamount to a empty variable.
also, I just noticed that you are using double quotes in your statement. I
have always had bad luck with using double quotes. I would switch to single
quotes.
how are you getting the $invoiceid value? is this code inside another a
larger loop?
Jim lucas
----- Original Message -----
From: "Ben C." <benc@cox.net>
To: <php-general@lists.php.net>; <curt@zirzow.dyndns.org>
Cc: <phplist@zonedzero.net>
Sent: Monday, August 04, 2003 10:33 PM
Subject: Sum a column of values from a MySQL query
($invoicepartpaid)> 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 invoicesreply> together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER.
>
> If anyone can help I would greatly appreciate it.
>
>
> --------------------------------------------------------------------------
> The content of this email message and any attachments are confidential and
> may be legally privileged, intended solely for the addressee. If you are
> not the intended recipient, be advised that any use, dissemination,
> distribution, or copying of this e-mail is strictly prohibited. If you
> receive this message in error, please notify the sender immediately by> email and destroy the message and its attachments.
>
>Jim Lucas Guest
-
Jason Wong #4
Re: [PHP] Re: Sum a column of values from a MySQL query
On Tuesday 05 August 2003 23:22, Jim Lucas wrote:
His sql statement assignment looks fine to me. Whether you mainly use> also, I just noticed that you are using double quotes in your statement. I
> have always had bad luck with using double quotes. I would switch to
> single quotes.
single-quotes or double-quotes is irrelevant to the end result as long as
they are used correctly. Luck has no part in determining whether your
statement is correctly formed. I just want to point this out so the OP
doesn't go barking up the wrong tree.
--
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 am an optimist. It does not seem too much use being anything else.
-- Winston Churchill
*/
Jason Wong Guest



Reply With Quote

