2

I'm having problem while fetching data from database and converting into json using codeigniter. I've following database

create table todolist(
todo_id int,
todo_content text
)

with these example entity in database

1        assignment
2        lab report
3        notes making
4        preparing meal
5        cleaning room
6        feeding pet

I have written following function in Todo_Controller

public function json(){
$content = $this->db->get('todolist'); //todolist table name
echo  json_encode($content);
}

I'm getting following output

{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":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}

your help will be highly appreciated.

6 Answers 6

1

You can use result_array() which returns the query result as a pure array. You can easily access your data from client side using json_encode().

public function json(){
    $content = $this->db->get('todolist'); //todolist table name
    $data = $content->result_array();
    echo  json_encode($data);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Please add some explanation to your code. E.g., what does $content->result_array(); do?` Why is it needed to call it?
@Jost I have edited answer, it returns the query result as a pure array, so it will be helpful to access it from client side.
@SangamJungGauli if you find any answer useful or you get solution then please select answer.
1
public function json(){
    $content = $this->db->get('todolist'); //todolist table name

    foreach ($content->result() as $row)
    {
        echo  json_encode($row);
    }

}

Try using loop and result function to fetch result. And its better to use active records queries in model.

You can read the documention for Active Records from here

Make a model and load it in your controller or if you want that model in all over your project then load it in autoload file.

Make a function on MODEL like this.

Public function getData(){
  $content = $this->db->get('todolist'); //todolist table name

   foreach ($content->result() as $row)
     {
      return $row;
     }    
}

And call it on your Controller's function like this

public function json(){
$content = $this->YOUR_MODEL_NAME->getData();
$this->load->view('YOUR_FILE_NAME',$content);
}

Comments

0

Here you go:

public function json()
{
    $sql = 'SELECT * FROM todolist';
    $q = $this->db->query($sql);

    //Fetch the result array from the result object and return it
    return $q->result();
}

Comments

0

Take a look at this. Should work fine.

1 Comment

@SangamJungGauli glad I could help. You can rate the answer tho.
0

Try this code:

Model

function get_people() {
  static $query;
  $this->db->select('id, name');
  $query = $this->db->get('people');
  if($query->num_rows() > 0) return $query->result();
  else return FALSE;
}

Controller

function index(){
  header('Content-type: application/json');
  echo json_encode($this->my_model->get_people());
}

Comments

0

You can do it like this :

public function json(){
  $content = $this->db->get('todolist')->result_array(); //todolist table name
  echo  json_encode($content);
}

1 Comment

You can up vote and accept it as answer if it helped as it will help others too

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.