I'm trying to make a dynamic query using start/end date filters to display units per date. I already query which rows are available between the start and end date in the database and it creates an array like: (array ("2020-03-01", "2020-03-02", 2020-03-03"). When I want to use this array in another foreach to get the related units per day, I won't get any result. The big question is: What am I doing wrong?
The first while loop:
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$date = $row["datetime"];
$filterDates[] = '"'.$row["datetime"].'", ';
}
}
When using this foreach I get results.
foreach (array("2020-03-10 00:00:00", "2020-03-11 00:00:00", "2020-03-12 00:00:00", "2020-03-13 00:00:00", "2020-03-14 00:00:00", ) as &$sqlDate)
When using this foreach I won't get results.
foreach (($filterDates) as &$sqlDate)
This is the 'dynamic' query I'm using to get the units per day
$sql = "SELECT `datetime`, `period`, `units` , SUM(`Units`) AS 'units' FROM `Subscriptions_raw` WHERE `datetime` LIKE '%".$sqlDate."%' AND `period` = '1 Month' GROUP BY `datetime`";
$result = $mysqli->query($sql);
for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row);
echo $set[0]["units"].", ";
}
LIKEon yourdatetime? What is that column's type? Also, why are you adding extra quotes here:$filterDates[] = '"'.$row["datetime"].'", ';?