Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

take all numbers in a column from DB and add their value

Codes here !

Moderators: macek, egami, gesf

take all numbers in a column from DB and add their value

Postby Godiwa » Wed Mar 05, 2003 6:57 am

I would like to know if anyone can help me with this.

I have a DB where a lot of numbers have been entered, and I would like to take all the numbers in column "testinfo" to be added

how do I draw all the numbers out from the DB and add them and make a $testinfo var with the value of all of them?

I am a newbie, but I got the most of my scribt working now and I got to the part to draw it from the DB and do the addition part.

Jesper
Godiwa
New php-forum User
New php-forum User
 
Posts: 40
Joined: Wed Mar 05, 2003 5:55 am

Postby Godiwa » Wed Mar 05, 2003 2:36 pm

this would be the plan, but I don't know how to write it so it works:

Code: Select all
$result = mysql_query("SELECT my_col FROM my_tbl")

gets the numbers from my_col, so now I got the numbers, then I want to take the numbers from my_col and add them to eachother so I end up with one number

my_col
ID1 103
ID2 109
ID3 108

the result of that would be 320 when you add them to eachother and that result I would like to get into $myvar

anyone that can help me with that please?
Godiwa
New php-forum User
New php-forum User
 
Posts: 40
Joined: Wed Mar 05, 2003 5:55 am

Postby Joan Garnet » Sat Mar 08, 2003 5:46 am

I think that's it:
Code: Select all
$result = mysql_query("SELECT my_col FROM my_tbl");
while  ( $row= mysql_fetch_array ( $result ) ){
    $my_number = explode (" ", $row["my_col"]);
    $sum += $my_number[1];
}
echo $sum;

try it... I think it should work..
:)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron