The below code returns an array filled with the number of users last seen for each day for $dayCount
number of days that is specified via a parameter into the function.
It does this by executing a database query for each day from the previous day going back $dayCount
amount of days, and another function produces from this data to provide an output in CSV-style formatting for a graph.
This essentially means that if, say, I wanted the number of users last seen on each day for the last thirty days, a total of 30 database queries will be run.
To me this seems incredibly inefficient, but I can't think of a way around this myself. Can anything be done to make this more efficient?
function GetUserCountFromDays($dayCount) {
$arrCnt = array();
$baseQuery;
$result;
for ($day = $dayCount; $day >= 1; $day--) {
$baseQuery = "SELECT * FROM example.servers WHERE last_seen >
UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL -" . $day . " DAY))";
$result = ConnectToMySQL()->query($baseQuery);
if ($result){
array_push($arrCnt, $result->num_rows);
}
}
return $arrCnt;
}