Cream Whipped Airplane
Cream Whipped Airplane

Reputation: 1367

How to echo query results in my view?

I must say I am really, really stupid at understanding this logic. I've been playing around with this test for almost an hour now and I can't get it to work the way I want to. I'm either getting "trying to get property of non-object" or "array to string conversion" errors.

My goal was to call a SQL query and have the results stored in a variable which I will then provide to my view. In the view I wanted to be able to retrieve the values from the variable like this: $info->email for example, to get the value that was stored in the email column in my table.

I have also tried to first do $query = $this->db->get('users'); return $query->row();, but this didn't work either. Please excuse my stupidity, and if you could be so kind and explain to me how I can achieve what I described above. The test code is below. Also, this query always finds only 1 row 100% as the user id is unique.

Controller

$data['info'] = $this->User_m->get_my_info($this->user_id);
$this->load->view('test_v', $data);

Model

private function get_my_info($uid){
    $this->db->select('id, name, email');
    $this->db->where('user_id', $uid);
    return $this->db->get('users'); 
}

View

<p>Email: <?php echo $info->email ?></p>

Upvotes: 0

Views: 227

Answers (4)

Jahanzeb
Jahanzeb

Reputation: 613

There are 2 changes you should made in your model.

1- Function will be public

2- row() called in return.

Hope it will help you.

Model

public function get_my_info($uid){
    $this->db->select('id, name, email');
    $this->db->where('user_id', $uid);
    return $this->db->get('users')->row();  
}

Upvotes: 0

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

You should take a look at this. Generating Query Results.You should return something

format      |   object  |   array
-------------------------------------
single      |   row()   |   row_array()
multiple    |   result()|   result_array()

You should choose format according to your requirements. Your function should be like this as you are retrieving single user.

public function get_my_info($uid)
{
    return $this->db
                ->select('id, name, email')
                ->where('user_id', $uid)
                ->get('users')
                ->row();
}

Also the model function should be public if you keep it private it will be only accessible in the model class and not with the controller.
you can enable profiler to see what queries are running.
Just add this line to your controller constructor.

$this->output->enable_profiler(true);

Upvotes: 2

user2727841
user2727841

Reputation: 725

you should use function which return values for example.

return $this->db->get('users')->row();
return $this->db->get('users')->result();
return $this->db->get('users')->result_array();

and function should be public not private

Upvotes: 0

Nouphal.M
Nouphal.M

Reputation: 6344

Make the model function public instead of private

Then in view

if ($info->num_rows() > 0) {
    foreach ($info->result() as $row)  {
        echo $row->email;
    }
}

See more infor here

Upvotes: 1

Related Questions