From 657320c306cf24b5bb4b23faa7ef43f06f161520 Mon Sep 17 00:00:00 2001 From: Tatiana Villa Ema Date: Tue, 17 Feb 2026 19:29:11 +0100 Subject: [PATCH] resolviendo errores --- eltiempo/servidor/api-weather-fechas.php | 2 - .../servidor/api-weather-fechas20260217.php | 150 ++++++++++++++++++ 2 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 eltiempo/servidor/api-weather-fechas20260217.php diff --git a/eltiempo/servidor/api-weather-fechas.php b/eltiempo/servidor/api-weather-fechas.php index d99d7c6..1e60bd1 100644 --- a/eltiempo/servidor/api-weather-fechas.php +++ b/eltiempo/servidor/api-weather-fechas.php @@ -114,8 +114,6 @@ $sql .= " ORDER BY DATE(fecha); "; -console.log("SQL: $sql"); -console.log("Params: " . implode(", ", $params) . "\n"); // ============================ // 6. PREPARAR Y EJECUTAR // ============================ diff --git a/eltiempo/servidor/api-weather-fechas20260217.php b/eltiempo/servidor/api-weather-fechas20260217.php new file mode 100644 index 0000000..5cacec0 --- /dev/null +++ b/eltiempo/servidor/api-weather-fechas20260217.php @@ -0,0 +1,150 @@ + "El parámetro 'ciudad' es obligatorio."]); + exit(); +} + +// Validar formato fecha YYYY-MM-DD +function fechaValida($fecha) { + return preg_match('/^\d{4}-\d{2}-\d{2}$/', $fecha); +} + +if (!empty($fecha) && !fechaValida($fecha)) { + http_response_code(400); + echo json_encode(["error" => "El parámetro 'fecha' debe tener formato YYYY-MM-DD."]); + exit(); +} + +if (!empty($desde) && !fechaValida($desde)) { + http_response_code(400); + echo json_encode(["error" => "El parámetro 'desde' debe tener formato YYYY-MM-DD."]); + exit(); +} + +if (!empty($hasta) && !fechaValida($hasta)) { + http_response_code(400); + echo json_encode(["error" => "El parámetro 'hasta' debe tener formato YYYY-MM-DD."]); + exit(); +} + +// ============================ +// 4. CONEXIÓN BD +// ============================ +$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); + +if ($conn->connect_error) { + http_response_code(500); + echo json_encode(["error" => "Error de conexión a la base de datos."]); + exit(); +} + +// ============================ +// 5. CONSTRUIR QUERY DINÁMICA +// ============================ +$sql = " + SELECT + DATE(fecha) AS dia, + MIN(fecha) AS primera_fecha_del_dia, + MIN(amanecer) AS amanecer, + MAX(anochecer) AS anochecer, + MAX(temp_max) AS temp_max, + MIN(temp_min) AS temp_min, + AVG(humedad) AS humedad, + SUM(lluvia) AS lluvia, + AVG(nubes) AS nubes, + AVG(viento_velocidad) AS viento_velocidad, + AVG(viento_direccion) AS viento_direccion + FROM weather + WHERE ciudad LIKE CONCAT('%', ?, '%') +"; + +$params = [$ciudad]; +$types = "s"; + +// Filtro por fecha exacta +if (!empty($fecha)) { + $sql .= " AND DATE(fecha) = ?"; + $params[] = $fecha; + $types .= "s"; +} + +// Filtro por rango +if (!empty($desde) && !empty($hasta)) { + $sql .= " AND DATE(fecha) BETWEEN ? AND ?"; + $params[] = $desde; + $params[] = $hasta; + $types .= "ss"; +} elseif (!empty($desde)) { + $sql .= " AND DATE(fecha) >= ?"; + $params[] = $desde; + $types .= "s"; +} elseif (!empty($hasta)) { + $sql .= " AND DATE(fecha) <= ?"; + $params[] = $hasta; + $types .= "s"; +} + +$sql .= " + GROUP BY DATE(fecha) + ORDER BY DATE(fecha); +"; +echo $sql; // Para depuración: muestra la consulta generada +// ============================ +// 6. PREPARAR Y EJECUTAR +// ============================ +$stmt = $conn->prepare($sql); + +if (!$stmt) { + http_response_code(500); + echo json_encode(["error" => "Error al preparar la consulta."]); + $conn->close(); + exit(); +} + +$stmt->bind_param($types, ...$params); +$stmt->execute(); +$result = $stmt->get_result(); + +$datos = []; + +while ($row = $result->fetch_assoc()) { + $datos[] = $row; +} + +// ============================ +// 7. RESPUESTA +// ============================ +http_response_code(200); +//echo json_encode($datos); + +// ============================ +// 8. CIERRE +// ============================ +$stmt->close(); +$conn->close(); +?>