user1024742
user1024742

Reputation: 108

Codeigniter $this->db->where() not working

$this->db->from('posts');
$this->db->where('id',$this->uri->segment(3));
$data['query'] = $this->db->get();

I am trying to get one post using a where statement. I don't get any errors when I run this code. But in my view when I try to get $query->result()->body PHP gives me an error Trying to get property of non-object. So I ran print_r($query) and this is what it gave me:

(
    [conn_id] => Resource id #27
    [result_id] => Resource id #32
    [result_array] => Array
        (
        )

    [result_object] => Array
        (
        )

    [custom_result_object] => Array
        (
        )

    [current_row] => 0
    [num_rows] => 0
    [row_data] => 
)

I have no idea why it won't get my specific post from the database. I know my database is in working condition. I use it elsewhere in my site. I also use $this->db->where() elsewhere on my site and it works perfect. I've also tried plugging in id's of posts instead of $this->uri->segment(3) and I get the same problem. What am I doing wrong?

Upvotes: 1

Views: 3749

Answers (1)

Draex_
Draex_

Reputation: 3474

result() should be used when you are selecting more than 1 row from db, use row() instead.

In controller use

$data['query'] = $this->db->get()->row();

And then in view

$query->body

I strongly reccomend you to use $data['post'] and $post->body instead of $query->body. When you will have more queries on one page, your view will get messy.

Upvotes: 2

Related Questions