23 ) { $current_hour = 0; } } } /** * Fetches the temperature and humidity from the database */ function fetch_data ($client_id, $to, $from) { $temperature_data = []; $humidity_data = []; try { $dbh = new PDO('mysql:dbname=' . MYSQL_DATABASE . ';host=' . MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD); $sth1 = $dbh->prepare('SELECT AVG(`value`) AS `avg_value`, HOUR(`created_at`) AS `hour`, `created_at` FROM `temperature` WHERE `client_id` = :client_id AND `created_at` >= :from AND `created_at` <= :to GROUP BY `hour`'); $sth2 = $dbh->prepare('SELECT AVG(`value`) AS `avg_value`, HOUR(`created_at`) AS `hour`,`created_at` FROM `humidity` WHERE `client_id` = :client_id AND `created_at` >= :from AND `created_at` <= :to GROUP BY `hour`'); $sth1->execute([':client_id' => $client_id, ':to' => $to, ':from' => $from]); $sth2->execute([':client_id' => $client_id, ':to' => $to, ':from' => $from]); $temperature_data = $sth1->fetchAll(PDO::FETCH_ASSOC); $humidity_data = $sth2->fetchAll(PDO::FETCH_ASSOC); } catch ( PDOException $e ) { echo 'Connection failed: ' . $e->getMessage(); } return ['temperature' => $temperature_data, 'humidity' => $humidity_data]; } ?> Temperature and humidity for period <?php echo $data_period; ?>
No data from client during the period .