PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE webinars ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, description TEXT, date DATETIME NOT NULL, status TINYINT NOT NULL COMMENT 'Webinar status (1: Active, 0: Inactive)', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time', updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last update time' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; INSERT INTO webinars (title, description, date, status) VALUES ('Webinar 1', 'Description for Webinar 1', '2024-10-05 14:00:00', 1), ('Webinar 2', 'Description for Webinar 2', '2024-11-15 16:00:00', 0), ('Webinar 3', 'Description for Webinar 3', '2024-12-20 18:00:00', 1);
Copy Clear
Copy Format Clear
<?php $dbConfig = [ 'host' => '127.0.0.1', 'dbname' => 'webinarDB', 'username' => 'your_username', 'password' => 'your_password', 'port' => 3306 ]; $authEnabled = false; // You can enable/disable authentication by this setting. $apiKeys = [ 'key1' => 'your_secret_api_key1', 'key2' => 'your_secret_api_key2', 'key3' => 'your_secret_api_key3' ]; header('Content-Type: application/json'); if ($authEnabled && !authenticate($apiKeys)) { http_response_code(401); echo json_encode(['error' => 'Unauthorized access. Invalid API key.']); exit; } try { $status = getStatusFromRequest(); $webinars = fetchWebinars($dbConfig, $status); echo json_encode($webinars); } catch (Exception $e) { http_response_code(500); echo json_encode(['error' => 'Server error']); } /** * Check API Keys * * @param array $keys List of valid API keys * @return bool True if authentication is successful, false otherwise */ function authenticate($keys) { $headers = getallheaders(); $apiKey = $headers['Authorization'] ?? $_GET['api_key'] ?? ''; return in_array($apiKey, $keys); } /** * Get the status value from the request * * @return int|null Status value or null */ function getStatusFromRequest() { $status = null; if (isset($_GET['status'])) { $status = (int)$_GET['status']; } else { $jsonInput = file_get_contents('php://input'); $data = json_decode($jsonInput, true); if (is_array($data) && isset($data['status'])) { $status = (int)$data['status']; } } return $status; } /** * Fetch webinars from the database * * @param array $config Database configuration information * @param int|null $status Status filter * @return array List of webinars * @throws Exception In case of an error */ function fetchWebinars($config, $status) { $query = "SELECT * FROM webinars"; if ($status !== null) { $query .= " WHERE status = ?"; } $stmt = $mysqli->prepare($query); if ($status !== null) { $stmt->bind_param("i", $status); } if (!$stmt->execute()) { throw new Exception('Failed to execute the query: ' . $stmt->error); } $result = $stmt->get_result(); $webinars = []; while ($row = $result->fetch_assoc()) { $webinars[] = $row; } $stmt->close(); $conn->close(); return $webinars; }
Show:  
Copy Clear