I have a problem with my php query for getting data. Let me explain what I want. I have a database which saves the active users for some hours in a day. For example 01-01-2012 13:00
active = 5 01-01-2012 14:00 active = 10. My php query should make an array which contains 2 columns which are date and active. But date must be like 01-01-2012 withour hours. So I grouped them as date2 but I couldn’t find the active(sum) for each days. Here is my query which doesn’t give the right active sums.
$query2 = mysql_query("SELECT DATE_FORMAT(date, '%Y-%m-%d') AS date2, SUM(active) FROM hit WHERE game= '".$game."' AND source = '".$source."' AND date > '".$dateFrom."' AND date < '".$dateTo."' GROUP BY date2 ORDER BY date2");
while($tuple= mysql_fetch_array($query2)){
$myArr[] = $tuple;
}
print_r($myArr);
You need to alias
SUM(active)andGROUP BY date. Ifdateis adatetime, you canGROUP BY DATE(date). Also, be careful with reserved words and make sure to use tick marks where necessary.Further, if you are looking for an inclusive date range, you should know that when you send
2012-10-03, it is actually2012-10-03 00:00:00and any records between00:00:00and23:59:59will be ignored.Lastly, you should stop using
mysql_functions as they are being deprecated.This query should work:
Your array (
$myArr) would then look something like:Array ( [0] => Array ( [date2] => 2012-10-03 [active] => 5 ) )