2

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.

9
  • try $query->result() Commented Jun 16, 2019 at 4:06
  • @Saeed.Gh Tried, still the same Commented Jun 16, 2019 at 4:27
  • provide what do you have in result and which version of Codeigniter you use Commented Jun 16, 2019 at 4:29
  • @Saeed.Gh v3, btw, from the documents, resualt()===result_object() Commented Jun 16, 2019 at 4:33
  • what result_object() provides? error/null object ? Commented Jun 16, 2019 at 4:44

4 Answers 4

0

I think I find the reason, in model

return $query->result_array(); // also can be changed to return $query->result_object(); or return $query->result();

They can return array or object, and in controller file

$sql = $this->user_model->userdetail($data);

$sql should be array or object, but actually it return a Multidimensional Arrays like this

return result() or result_object()

Array
(
    [0] => stdClass Object
        (
            [email] => [email protected]
            [name] => lily
        )

)

return result_array()

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [name] => lily
        )

)

it's strange.

Sign up to request clarification or add additional context in comments.

Comments

0

In codeigniter $query->result_array(); is used to return result in array Format.

To get result in object format use $query->result(); like

if ($query->num_rows() > 0) {
        return $query->result();
    }

For single row use $query->result_array()

Comments

0

Maybe you need JSON_FORCE_OBJECT in json_encode.

echo json_encode(array(
        "status" => "0",
        "message" => "",
        "data" => $sql), JSON_FORCE_OBJECT);

Comments

0

Your problem is you are using result_array(). You have to use row() instead, row() function will give you the object instead of array.

The model code:

function userdetail($data) {
    $id = $data["id"];
    $sql = "select email, name from user where id='".$id."'";
    $query = $this->db->query($sql);
    return $query->num_rows() > 0 ? $query->row() : 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.