<?php
// Connecting, selecting database
$dbconn = pg_connect("host=localhost dbname=nws user=nws password=nws")
    or die('Could not connect: ' . pg_last_error());

$camid=$_GET['camid'];

// Performing SQL query
// Always treat hydro and airport as booleans - convert to true/false
$query = "SELECT *, COALESCE(hydro, false) as hydro, COALESCE(airport, false) as airport FROM cams WHERE camid = '{$camid}'";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());

// Printing results in HTML
$array = array();
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    // Ensure hydro is a proper boolean
    $line['hydro'] = ($line['hydro'] === 't' || $line['hydro'] === true);
    // Ensure airport is a proper boolean
    $line['airport'] = ($line['airport'] === 't' || $line['airport'] === true);
    $array[] = $line;
}

// Debug: log the data being sent
error_log("Single.php response for camid $camid: " . json_encode($array));

echo json_encode($array);

// Free resultset
pg_free_result($result);

// Closing connection
pg_close($dbconn);
?>

