'success', 'data' => $data, 'count' => count($data) ]; // Free resultset pg_free_result($result); } catch (Exception $e) { http_response_code(500); $response = [ 'status' => 'error', 'message' => $e->getMessage() ]; } // Output JSON echo json_encode($response, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK); // Close database connection when needed if (isset($dbconn)) { pg_close($dbconn); } ?>