PHP For循环到日期范围不起作用

I'm trying to loop through every day in a month and get the number of results for each day in one month.

I've done the following;

<?php
include 'inclues/db.inc.php';

for($i = 2; $i < 30; $i++){
    $date2 = $i-1;
    $date1 = $i;

    $q = mysql_query("SELECT * FROM `table` WHERE `date` < '2012-09-".$date1." 00:00:00' AND `date` > '2012-09-".$date2." 00:00:00'";
    //$r = mysql_fetch_assoc($q);
    echo mysql_num_rows($q)."<br />";
}

?>

It works if I just try to echo out dates 1 and 2 but not if I use the query.. I end up with a 500 internal server error when using the query.

Any ideas on how to resolve this? Is it generally bad practice to use a query within a loop?

Just execute

SELECT date, COUNT(*) from table group by date

It's not bad practice to use a query within a loop.

Check what your query-string is becomming, try running that query manually on your db. It probably produses some error.

 $sql = "SELECT * FROM `table` WHERE `date` < '2012-09-".$date1." 00:00:00' AND `date` >      '2012-09-".$date2." 00:00:00'";
 echo $sql;
 $q = mysql_query($sql);
SELECT * FROM `table` WHERE `date` BETWEEN $date1 AND $date2;