Codeigniter - 尝试获取非对象的属性

时间:2013-06-16 03:42:34

标签: php codeigniter

请帮助解决我在Codeigniter和杂货店中遇到的错误。下面的代码抛出以下信息,我坚持了几天,我是一个总菜鸟:( 错误:

遇到PHP错误

严重性:注意

消息:尝试获取非对象的属性

文件名:controllers / examples.php

行号:70

PS:

功能可以满足需要,但会出现上述错误。

感谢您对此的看法!

 function security() {
 $method = $this->uri->segment(3); //tell ci that we are working on url segment 3, e.g. delete,  update ....

if ($method == "edit" or $method == 'update_validation' or $method == 'delete') {
       $id = $this->uri->segment(4); //work on url segment 4, now pointing at posts table       primary key
        $this->db->where('posts.user_id', $this->session->userdata('id'));
        $result = $this->db->get_where('posts', array('id' => $id), 1)->row();

       //this is line: 70 if ($result->id != $id)
       {
           echo "You don't have access";
           exit;
       }
     else return true;
 }
}

1 个答案:

答案 0 :(得分:0)

您可能需要在$this->db->get_where('posts', array('id' => $id), 1)->row();行添加值检查。看起来这行可能会返回false或者其他东西,如果没有行可用,但我在文档中找不到确切的返回值。

$result = this->db->get_where('posts', array('id' => $id), 1)->row();
if (!$result || $result->id != $id) {
  echo "You don't have access";
  exit;
}