<?php

include('db.php');

// Check the connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}

try {
    $sql = "SELECT ps.Id, ps.Name, ps.Postal_address, ps.Email_address, ps.Mobile_number, ps.Prefered_ctime, ps.Date, ps.P_address, ps.P_bedrooms, ps.P_bathrooms, ps.P_car_spaces, ps.P_special_features, GROUP_CONCAT(i.images) AS P_image FROM property_sale AS ps LEFT JOIN images AS i ON ps.Id = i.record_id GROUP BY ps.Id, ps.Name, ps.Postal_address, ps.Email_address, ps.Mobile_number, ps.Prefered_ctime, ps.Date, ps.P_address, ps.P_bedrooms, ps.P_bathrooms, ps.P_car_spaces, ps.P_special_features;";
    $result = $con->query($sql);

    if ($result === false) {
        throw new Exception("SQL query error: " . $con->error);
    }

    if ($result->num_rows > 0) { 
        $items = array();
        // output data of each row
        while ($row = $result->fetch_assoc()) {
            $items[] = $row;
        }
        $response = array('status' => "true", 'Message' => "Fetched Data Successfully", 'Property_Sale' => $items);
        echo json_encode($response);
    } else {
        $response = array('status' => "false", 'Message' => "No Data");
        echo json_encode($response);
    }
} catch (Exception $e) {
    $response = array('status' => "false", 'Message' => "SQL Error: " . $e->getMessage());
    echo json_encode($response);
}

$con->close();
?>
