1
$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?

1
  • Your database-accessing model methods should not be directly accessing $this->uri->segment(3) (url parameters). Any data that a model method requires should be passed to it (usually by the controller method which calls the model). It looks like you should be using get_where() ActiveRecord method call for your task. If you want to return a single record as an object or null if there is no row -- use row(). For an flat array, you can use row_array(). Commented Dec 6, 2024 at 2:51

1 Answer 1

2

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.

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

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.