-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_connection.php
More file actions
28 lines (23 loc) · 870 Bytes
/
Copy pathtest_connection.php
File metadata and controls
28 lines (23 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
// 1. Include our database file (which sets up $pdo)
require_once __DIR__ . '/database.php';
// 2. Set the response header to JSON format
header('Content-Type: application/json');
try {
// 3. Execute a simple raw SQL query to test the active connection socket
$stmt = $pdo->query("SELECT VERSION()");
$version = $stmt->fetchColumn();
// 4. Output a successful connection message along with the DB engine details
echo json_encode([
"status" => "success",
"message" => "PHP successfully connected to the PostgreSQL Docker container!",
"database_version" => $version
]);
} catch (\PDOException $e) {
// 5. Handle any runtime execution errors gracefully
http_response_code(500);
echo json_encode([
"status" => "error",
"message" => "Query execution failed: " . $e->getMessage()
]);
}