4
votes

so I'm having this issue not being able to get just a string from a table. This is my method in the controller:

    public function get_tree($id){
        $data['tree'] = $this->Product_model->get_tree($id);
        return $data;
    }

And this is the function in the model:

function get_tree($id){
    $this->db->select('ascending_path');
    $this->db->from('category');
    $this->db->where('id', $id);
    $result = $this->db->get();
    return $result;
}

I'm showing this with Ajax into the view but nothin seems to show up. There is not even an error, 200 status code and the request is shown in the access log. Any hints?

PS: if I try to json_encode it and then pass the dataType:json in the ajax call all it comes back is:

{"tree":{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null},"result_id":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"result_array":[],"result_object":[],"custom_result_object":[],"current_row":0,"num_rows":null,"row_data":null}}

Everything seems to be null. I don't even know what is that object, I mean, the query is supposed to bring just one string.

2
Check this answer. Seems might help you.Tpojka
You need to pass type to your AJAX as JSONP which requires json_encode($data) from controller. Anything else is what are you getting from model, so you should check it in if else loop like in link I just showed you. Issue in that question could lead you to answer. Btw, have you sorted problem or not?Tpojka
No, not yet. I did also tried to query it manually in mysql with select ascending_path from category where id=(somerealid); and that brings the data I need, so I know the query is right too.syrano
That object is just what have you got from model. So you would check your model method rather.Tpojka
That's the actual model method. The object is saying that the rows afected were 0 when I know it's not if I permform the query manually in the terminal.syrano

2 Answers

2
votes

Change return $result; to return $result->result();

Read more about Generating Query Results here

1
votes

use this

 function get_tree($id){
     $query = $this->db->query("SELECT ascending_path FROM category WHERE id ='$id'");
     $result = $query->result_array();
     return $result;
 }