从数据库中获取数据时出现错误-未定义属性:CI_Loader::$m


getting an error when fetching data from database - Undefined property: CI_Loader::$m

我在查看页面时遇到了错误,我是代码编写者,请帮助我…

这个在视图页

foreach($this->m->gettable() as $row)
{
    echo "<tr>
    <td>$row->id</td>
    <td>$row->studentname</td>
    <td>$row->gender</td>
    <td>$row->phone</td>
    <td></td>
    </tr>";
}

模型页

function gettable()
    {
        $query=$this->db->get('tblstudent');
        return $query->result();
    }

在控制器页

public function _construct()
{
        parent::_construct();
        //call model
        $this->load->model("StudentModel","m");
    }
    function index()
    {
        $this->load->view("index");
    }
    function savedata()
    {
        //create array for get data from index
    $data=array(
                      'studentname' => $this->input->post('studentname'),
                      'gender'  =>  $this->input->post('gender'),
                      'phone'  =>  $this->input->post('phone')
                   );

     //mean that insert into database table name tblstudent
        $this->db->insert('tblstudent',$data);
    //mean that when insert already it will go to page index    
        redirect("Student/index");
    }

语法是__construct加2x下划线,而不是1x _construct

  • http://php.net/manual/en/language.oop5.decon.php

对它的所有实例进行更改,并使用错误报告:

  • https://ellislab.com/codeigniter/user-guide/general/errors.html
  • http://php.net/manual/en/function.error-reporting.php

更改控制器索引函数,如下所示

function index()
{
   $data['result']=$this->m->gettable();
   $this->load->view("index",$data);
}

在查看页面更改如下:

  foreach($result as $row)
 {
  echo "<tr>
  <td>$row->id</td>
  <td>$row->studentname</td>
  <td>$row->gender</td>
   <td>$row->phone</td>
        <td></td>
     </tr>";
      }