27 lines
691 B
PHP
27 lines
691 B
PHP
<?php
|
|
session_start();
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['error' => 'User not logged in.']);
|
|
exit;
|
|
}
|
|
|
|
$city_name = $_GET['city'] ?? null;
|
|
|
|
if (!$city_name) {
|
|
echo json_encode(['error' => 'City name is required.']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM weather_history WHERE city_name = ? ORDER BY timestamp DESC");
|
|
$stmt->execute([$city_name]);
|
|
$history = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
echo json_encode($history);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
|
}
|