Returning the number of records per day I'm writing a query to get the number of sessions created each day for the past 90 days. I'm currently using a prepared statement to do this - looks something like this:

for($n = 0; $n &lt; 90; ++$n){
    $days[] = $n;
}

$st = $this-&gt;db-&gt;prepare("
    SELECT * 
    FROM `sessions` 
    WHERE DATE(sessionStart) = DATE_SUB(CURDATE(), INTERVAL :n DAY)
");

$bind = array(':n' =&gt; null);
$data = array();

foreach($days as $day){
    $bind[':n'] = $day;
    $st-&gt;execute($bind);
    $data[$day] = $st-&gt;rowCount();
}


Would my code perform better using either of these?


This code, which is essentially running 90 separate queries
Running one query to return everything from the sessions table and then sorting the results in PHP
Aside from @Sumurai8 recommendations, the sql query can be indeed complexified a bit to return all the results in one go:

SELECT DATE_FORMAT(`sessionStart`, '%Y-%m-%d') AS `day`, COUNT(*) AS `sessions`
FROM `sessions` 
WHERE 
    DATE(`sessionStart`) BETWEEN DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND CURDATE()
GROUP BY
    `day`
ORDER BY
    `day`


It only needs to be executed once, and then iterated over the result set using your favorite FETCH_XXX method.