I meet a question in Codeignter when I try to get an object return, some of controllers codes are
$sql = $this->user_model->userdetail($data);
if ($sql) {
echo json_encode(array(
"status" => "0",
"message" => "",
"data" => $sql
));
exit();
}
And the model codes are
function userdetail($data) {
$id = $data["id"];
$sql = "select email, name from user where id='".$id."'";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
return $query->result_array();
}
return $query->num_rows();
}
I can get the result
{
"status": "0",
"message": "",
"data": [
{
"email": "[email protected]",
"name": "lily"
}
]
}
here the data is an array, but it should be an object, the above result should like this
{
"status": "0",
"message": "",
"data": {
"email": "[email protected]",
"name": "lily"
}
}
And I changed return $query->result_array(); to return $query->result_object(); in model code, but it doesn't work, what should I do here? Thanks a lot.