I am currently passing an object from PHP through a JSON, consisting of message, result and an object with 2 attributes: id and region name.
PHP function that performs select
and returns an object to the calling function:
public function getAll(){
$consulta='SELECT * FROM REGION';
try {
$query = $this->conn->prepare($consulta);
$query->execute();
$data = $query->fetchObject();
if($query){
$region["id_region"] = $data -> ID_REGION;
$region["nombre_region"] = $data -> NOMBRE_REGION;
return $region;
} else {
return false;
}
} catch (PDOException $e) {
return false;
}
This is finally the PHP that returns the JSON to the client:
public function listarRegion(){
$db = $this -> db;
$total = $db -> getAll();
if ($total) {
$response["result"] = "success";
$response["message"] = "Se adjunta lista de regiones";
$response["region"] = $total;
return json_encode($response);
} else {
//return json_encode(array(
$response["result"] = "failure";
$response["message"] = "No existe datos";
return json_encode($response);
}
}
This code works correctly with a record in the REGION
table that has id_region
and nombre_region
, however I need all regions to arrive in an array. How could I do that?