<?php
header("Access-Control-Allow-Origin: *");
include 'conexion.php';
$table = 'address';

$getJson = file_get_contents('php://input');
$json = json_decode($getJson);
$username = $json->username;

$sql = $connect->query("SELECT * FROM " . $table . " WHERE username='" . $username . "'");

$hasil = array();

while ($fetchSql = $sql->fetch_assoc()) {
    $hasil[] = $fetchSql;
}

if ($sql->num_rows > 0) {
    $response = array(
        'error' => false,
        'message' => null,
        'data' => $hasil
    );
} else if ($sql->num_rows == 0) {
    $sql = $connect->query("SELECT * FROM " . $table . " WHERE address_id = 1");


    while ($fetchSql = $sql->fetch_assoc()) {
        $hasil[] = $fetchSql;
    }
    $response = array(
        'error' => false,
        'message' => null,
        'data' => $hasil
    );
} else {
    $response = array(
        'error' => true,
        'message' => 'Error',
        'data' => null
    );
}

header('Content-Type: application/json');
echo json_encode($response);
