| Coding Forum Problems with your code? Let's hear about it. |
12-17-2006, 06:02 PM
|
#1 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
Latest Blog: None
|
Totaling Numbers PHP
How can I total colums in PHP from a database? Just a little script I can add into a current PHP script.
|
|
|
12-17-2006, 08:01 PM
|
#2 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
It might be a bit silly to do that in PHP, although there may be circumstances under which you'd opt for PHP rather than for SQL.
Why don't you try SQL first:
SELECT SUM(FieldName) FROM TableName
|
|
|
12-17-2006, 11:08 PM
|
#3 (permalink)
|
|
Inactive
Join Date: 03-04-06
Posts: 181
|
Adding columns? Do you mean rows?
I thought columns were made in the table structure...
Are you making columns on the fly with ALTER TABLE command?
Bye 
|
|
|
12-20-2006, 04:27 PM
|
#4 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
Quote:
Originally Posted by jeet
Adding columns? Do you mean rows?
|
Who's adding columns?
Quote:
Originally Posted by jeet
I thought columns were made in the table structure...
|
You thought correctly, but you have no idea about basic SQL syntax.
|
|
|
12-20-2006, 04:36 PM
|
#5 (permalink)
|
|
Inactive
Join Date: 03-04-06
Posts: 181
|
Quote:
Originally Posted by littleFella
Who's adding columns?
You thought correctly, but you have no idea about basic SQL syntax.
|
I'm confused... Didn't the thread starter say, "How can I total colums"?
And the thread title says "totaling numbers"?
Thanks for your nice words anyway...
Bye
Last edited by jeet : 12-20-2006 at 04:48 PM.
|
|
|
12-20-2006, 05:45 PM
|
#6 (permalink)
|
|
Contributing Member
Join Date: 07-08-06
Location: Here
Posts: 236
Latest Blog: None
|
Quote:
Originally Posted by jeet
I'm confused... Didn't the thread starter say, "How can I total colums"?
And the thread title says "totaling numbers"?
Thanks for your nice words anyway...
Bye
|
he sure had nice words for you didn't he?
anyway, yup doing it using an sql statement is much easier. but if you insist of doing it using php, you'll have to:
1. query the table
2. store the results in a recordset
3. iterate every record in the recordset and sum up the values
try the codelet below (i have not tested it - coded it right out of my head):
Code:
$sql = "select column from table where [your criteria here]";
$rst = mysql_query($sql);
while($row = mysql_fetch_array($rst)) :
$total = $total + $row['column'];
endwhile;
print "Total = " . $total;
|
|
|
12-20-2006, 06:56 PM
|
#7 (permalink)
|
|
Inactive
Join Date: 03-04-06
Posts: 181
|
Quote:
Originally Posted by daboss
he sure had nice words for you didn't he? 
anyway, yup doing it using an sql statement is much easier. but if you insist of doing it using php, you'll have to:
1. query the table
2. store the results in a recordset
3. iterate every record in the recordset and sum up the values
try the codelet below (i have not tested it - coded it right out of my head):
Code:
$sql = "select column from table where [your criteria here]";
$rst = mysql_query($sql);
while($row = mysql_fetch_array($rst)) :
$total = $total + $row['column'];
endwhile;
print "Total = " . $total;
|
Oh, I get it now...
He wanted to total the "values" stored in a "column".
Hope I am right this time... 
Bye 
|
|
|
12-21-2006, 06:55 PM
|
#8 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
Latest Blog: None
|
Hmm...It does not seem to be working. The column name is "total" and the table is listings. Why isn't it working:
Quote:
<?
$sql = "listings";
$rst = mysql_query($sql);
while($row = mysql_fetch_array($rst)) : <----line 87
$total = $total + $row['total'];
endwhile;
print "Total = " . $total;
?>
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [file location] on line 87
Total =
|
|
|
|
12-21-2006, 07:44 PM
|
#9 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
out of curiosity; what's stopping you from doing this in just one SQL statement?
|
|
|
12-21-2006, 08:06 PM
|
#10 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
Latest Blog: None
|
What's the PHP code for that?
|
|
|
12-21-2006, 08:32 PM
|
#11 (permalink)
|
|
Inactive
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
Latest Blog: None
|
PHP Code:
<?php
$query_sum = "SELECT SUM(FiledName) FROM TableName ";
while($row = mysql_fetch_array($query_sum))
print "Total = " . $row;
?>
|
|
|
01-06-2007, 10:16 AM
|
#12 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
Latest Blog: None
|
I'm getting:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in xxx line 86.
Line 86:
<?php
$query_sum = "SELECT SUM(total) FROM listings ";
while($row = mysql_fetch_array($query_sum)) <<<Line 86
print "Total = " . $row;
?>
|
|
|
01-06-2007, 10:56 AM
|
#13 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
Latest Blog: None
|
Updated code; still same error:
PHP Code:
<?php $sql_querysum = 'SELECT SUM(total) FROM listings'; while($row = mysql_fetch_array($sql_querysum)) print $row
?>
|
|
|
01-07-2007, 04:49 PM
|
#14 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
Latest Blog: None
|
Anyone?
|
|
|
01-08-2007, 10:36 AM
|
#15 (permalink)
|
|
v7n Mentor
Join Date: 11-22-06
Location: Phoenix, AZ
Posts: 1,804
Latest Blog: None
|
SN3, you do need to change your code just a bit. Try this:
PHP Code:
<?php
$sql_querysum = 'SELECT SUM(total) FROM listings';
$sum_results = mysql_query($sql_querysum);
while($row = mysql_fetch_array($sum_results))
print $row
?>
__________________
Experimenting
|
|
|
01-08-2007, 12:21 PM
|
#16 (permalink)
|
|
Inactive
Join Date: 03-04-06
Posts: 181
|
If the above code does not work, try this one:
<?php
$sql= "SELECT total FROM listings";
$q = mysql_query($sql);
$l=0;
while($row = mysql_fetch_array($q)):
$l= $row['total'];
$l=$l+$l;
endwhile;
echo $l;
?>
Bye 
|
|
|
01-08-2007, 12:44 PM
|
#17 (permalink)
|
|
Contributing Member
Join Date: 11-01-03
Location: Kansas City
Posts: 1,067
|
lol.. Sorry to be a party buster, but hasn't the question been answered like 3 or 4 times already??  lol
__________________
█ Izzmo
█ Coding Guru Extraordinaire
█ ZeroWeb Hosting & Design - Customizable hosting for every type of user!
|
|
|
01-08-2007, 01:02 PM
|
#18 (permalink)
|
|
Inactive
Join Date: 03-04-06
Posts: 181
|
None of the codes worked...
Bye
Quote:
Originally Posted by Izzmo
lol.. Sorry to be a party buster, but hasn't the question been answered like 3 or 4 times already??  lol
|
|
|
|
01-08-2007, 02:09 PM
|
#19 (permalink)
|
|
Contributing Member
Join Date: 10-13-03
Location: Work, USA
Posts: 5,382
Latest Blog: None
|
Quote:
Originally Posted by jeet
If the above code does not work, try this one:
<?php
$sql= "SELECT total FROM listings";
$q = mysql_query($sql);
$l=0;
while($row = mysql_fetch_array($q)):
$l= $row['total'];
$l=$l+$l;
endwhile;
echo $l;
?>
Bye 
|
That worked. Thanks!
| |