views:

18

answers:

1

Hey, I have a tables called downloads, and in that table it stores the total amount of downloads for each day, so like

downloads  date
586  07-16-2010
906  07-17-2010
1019  07-18-2010
287  07-19-2010
15   07-20-2010
639  07-21-2010
337  07-22-2010

How could I retrieve the total number of downloads, so add them all up from every single day :)

Thanks!

+4  A: 

Would you believe it's as simple as

select sum(downloads) as total_downloads
  from downloads

EDIT

$query = 'select sum(downloads) as total_downloads from downloads';
$result = mysql_query($query);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
$row = mysql_fetch_assoc($result);
$total_downloads = $row['total_downloads'];
Mark Baker
it's giving me Resource id #32 any ideas? <-- nvm fixed that with mysql_fetch_row
Belgin Fish
of course you should add your where statement to limit it to the day to today ... more info on date time functions at http://reference.sitepoint.com/css/conditionalcomments#conditionalcomments__tbl_conditional-comments_operators
Elf King
of course it's giving you a resource id! it's returning the query handle not the result$res = mysql_query('select sum(downloads) as total_downloads from downloads'); $total_count= mysql_fetch_array($res, MYSQL_BOTH); more info at php.org
Elf King
@Belgin PHP's mysql_query() returns a resource comprising one or more rows of data. You need to fetch the first row (using a function like mysql_fetch_assoc()) and then get the value of total_downloads returned in the associative array returned by that
Mark Baker