Author |
Message |
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Sat Apr 26, 2003 3:33 pm (21 years, 8 months ago) |
|
Is there an easy way to add up the values of rows in a colum. |
|
|
|
|
SfCommand
Senior WebHelper
Joined: 10 Nov 2002
Posts: 143
Location: UK
|
Posted:
Sat Apr 26, 2003 3:40 pm (21 years, 8 months ago) |
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Sat Apr 26, 2003 8:32 pm (21 years, 8 months ago) |
|
Does this need to be queried, how do I put this into a script to display the sum. |
|
|
|
|
SfCommand
Senior WebHelper
Joined: 10 Nov 2002
Posts: 143
Location: UK
|
Posted:
Sat Apr 26, 2003 9:03 pm (21 years, 8 months ago) |
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Sat Apr 26, 2003 9:19 pm (21 years, 8 months ago) |
|
It echos Resource id #2 when I use this code, any ideas why?
Code: | $total = mysql_query("SELECT SUM(ab) from Robinson_Stats_2");
echo $total; |
The field as 3 rows 100, 69, 122, so it should echo 291 right? |
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Sat Apr 26, 2003 9:30 pm (21 years, 8 months ago) |
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Sat Apr 26, 2003 9:51 pm (21 years, 8 months ago) |
|
I dont get how to put this together, here is what I have and it echos Array
Code: | $total = mysql_query("SELECT SUM(ab) from Robinson_Stats_2");
$display = mysql_fetch_array($total);
echo $display; |
|
|
|
|
|
SfCommand
Senior WebHelper
Joined: 10 Nov 2002
Posts: 143
Location: UK
|
Posted:
Sat Apr 26, 2003 11:46 pm (21 years, 8 months ago) |
|
OK. Here's what I have that does what you want:
Code: | mysql_connect (<dbhost>, <db_user>, <db_pass>);
mysql_select_DB(<db_name>);
$total = mysql_query("SELECT SUM(someField) FROM someTable");
echo "$total[0]"; |
ooops, looks like I forgot to put the [0] bit in last time (guess that'll teach me to check that in the advert break between progs on BBC1
[edit]
Oops, looks like I should double check what I post to make sure I'm posting the right thing
[/edit] |
________________________________ Miguel
http://community.34sp.com
http://www.miguel.me.uk | http://www.sfcommand.co.uk | http://www.ssdg.org.uk
Last edited by SfCommand on Sun Apr 27, 2003 4:28 pm, edited 1 time in total |
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Sun Apr 27, 2003 2:25 am (21 years, 8 months ago) |
|
This is what I have and its not displaying anything.
Code: | $total = mysql_query("SELECT COUNT(ab) FROM Robinson_Stats_2");
echo "$total[0]";
|
I did connect to the database too. |
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Sun Apr 27, 2003 2:33 am (21 years, 8 months ago) |
|
I really need to find this out, I am supposed to have the stats part done by tomorrow their first game. So if anyone knows how to get this to work please let me know. |
|
|
|
|
drathbun
WebHelper
Joined: 01 Mar 2003
Posts: 69
Location: Texas
|
Posted:
Sun Apr 27, 2003 5:19 am (21 years, 8 months ago) |
|
Assuming that you have already connected to the database...
Code: | $sql = "SELECT count(ab) as at_bats
FROM Robinson_Stats_2";
if ( !($result = mysql_query($sql)) )
{
die('Could not obtain at bats information');
}
$row = mysql_fetch_assoc($result) ;
$total = $row['at_bats'];
|
By using mysql_fetch_assoc() you get an associative array of column results. You should name any column results that are calculated, like your count(), so that you have a descriptive name to work with. Then you reference the results using the array, indexed by column name.
Dave |
________________________________ Dave
Photography Site :: Query Tools Forum :: Weekend Fun |
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Sun Apr 27, 2003 4:15 pm (21 years, 8 months ago) |
|
This code tells me how many rows there are, im trying to add the content of the rows up.
Code: | $sql = "SELECT count(ab) as at_bats
FROM Robinson_Stats_2";
if ( !($result = mysql_query($sql)) )
{
die('Could not obtain at bats information');
}
$row = mysql_fetch_assoc($result) ;
$total = $row['at_bats'];
echo "$total"
|
|
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Sun Apr 27, 2003 4:23 pm (21 years, 8 months ago) |
|
As someone said above, replace: with |
________________________________
|
|
|
|
drathbun
WebHelper
Joined: 01 Mar 2003
Posts: 69
Location: Texas
|
Posted:
Sun Apr 27, 2003 8:36 pm (21 years, 8 months ago) |
|
Daniel wrote: | As someone said above, replace: with |
I was going with the code that was just a couple of posts above mine. If each "at bat" was stored as a row, then you would count them. If the ab column contains a number, then you sum them. It wasn't clear from your description which way you needed to go.
But I'm assuming that the code is working since you said you are getting data back now. At this point you have a template of sorts to get you going. Good luck!
Dave |
________________________________ Dave
Photography Site :: Query Tools Forum :: Weekend Fun |
|
|
|
|