简体   繁体   中英

Severity: Notice Message: Trying to get property of non-object error to show the data

I have a problem with CodeIgniter. This is the message:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/read.php

Line Number: 89

I have a model like this:

var $news = 'news';

function read($id)
    {
        $this->db->join('category', 'category.id_category = news.id_category');
        $this->db->join('editor', 'editor.id_editor = news.id_editor');
        $query = $this->db->get_where($this->news, array('id' => $id));
        return $query->row();
    }

This is my controller:

 ...
$data['news'] = $this->Newsmodel->read($id);
$this->load->view('read', $data);

And this is my view:

...
       <div class="news">
            <?php  echo $news->title; ?>
        </div>

How to solve this problem?

add $this->db->select('title); in your model's read() function's first line.

then replace return $query->row(); this from Your model with the below

return $query->result();

then replace echo $news->title; this in Your views with the below:-

echo $news[0]->title;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM