【问题标题】:getting an error when fetching data from database - Undefined property: CI_Loader::$m从数据库获取数据时出错 - 未定义属性:CI_Loader::$m
【发布时间】:2015-07-23 13:23:25
【问题描述】:

这里在查看页面上出现错误,我是 codeigniter 新手,请帮助我...

这个在查看页面

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");
    }

【问题讨论】:

  • 你的型号是什么?
  • 语法错误 /typo => _construct - 我在一小时内看到的第三个。你们都是从同一个地方获取相同的代码吗?! 叹息
  • @Bhandhavya Reddy 缩短你的问题标题

标签: php html codeigniter


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    更改您的控制器索引功能,如下所示

    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>";
    
          }
    

    这会起作用。

    【讨论】:

      猜你喜欢
      • 2014-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多