Connecting Tech Pros Worldwide Forums | Help | Site Map

add up mysql row and then echo the result

Familiar Sight
 
Join Date: Oct 2006
Posts: 142
#1: Aug 25 '07
Hi,

I have a database with a row called "time".

I need to somehow add up all the time rows and then echo out the total. How do I do it?

Example

MySQL
-----------
1. time 30
2. time 10
3. time 20

php
----------------


$total_time = ( addup all SQL rows FROM Time);

echo"$total_time min";
---------------------------------

Result:

60 min

Thanks

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#2: Aug 25 '07

re: add up mysql row and then echo the result


What type of a column is it? DateTime? Int?
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#3: Aug 26 '07

re: add up mysql row and then echo the result


Heya, webandwe.

Have a look at MySQL's date and time functions.
Familiar Sight
 
Join Date: Oct 2006
Posts: 142
#4: Aug 26 '07

re: add up mysql row and then echo the result


Hi, it is just a varchar....

I have a script that converts the numbers into time....I just need to write a php script that add's up all the number and then echo it out...I will hande the results from there.
entertainmentliveuk's Avatar
Newbie
 
Join Date: Aug 2007
Location: Dorset, UK
Posts: 9
#5: Aug 26 '07

re: add up mysql row and then echo the result


Quote:

Originally Posted by webandwe

Hi,

I have a database with a row called "time".

I need to somehow add up all the time rows and then echo out the total. How do I do it?

Example

MySQL
-----------
1. time 30
2. time 10
3. time 20

php
----------------


$total_time = ( addup all SQL rows FROM Time);

echo"$total_time min";
---------------------------------

Result:

60 min

Thanks

Try something like this:
[PHP]
$mins=0;
$time = mysql_query("select * from $table WHERE Time> '0' ");
{
while($row = mysql_fetch_array($time))
{
$mins=$mins+$row['Time'];
}
echo $mins." minutes";
[/PHP]
Reply