34566-vm/get_bookings.php
2025-10-02 23:39:28 +00:00

24 lines
830 B
PHP

<?php
require_once 'db/config.php';
try {
$pdo = db();
// Insert sample data if the table is empty
$stmt = $pdo->query('SELECT COUNT(*) FROM bookings');
if ($stmt->fetchColumn() == 0) {
$pdo->exec("INSERT INTO bookings (title, start_time, end_time) VALUES ('Booking 1', '2025-10-02 10:00:00', '2025-10-02 11:00:00')");
$pdo->exec("INSERT INTO bookings (title, start_time, end_time) VALUES ('Booking 2', '2025-10-02 14:00:00', '2025-10-02 15:00:00')");
}
$stmt = $pdo->query('SELECT title, start_time AS start, end_time AS end FROM bookings');
$bookings = $stmt->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json');
echo json_encode($bookings);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}