出现 null 错误时调用成员函数 get()

2023-12-12

我在 CodeIgniter-2.2.1 中不断收到此错误。(致命错误:在第 6 行的 C:\xampp\htdocs\ci\application\models\site_model.php 中对 null 调用成员函数 get())我不是确定为什么会发生这种情况。我正确调用该函数吗?第 6 行是 $this->load->model('site_model');

控制器站点.php

<?php
 class Site extends CI_Controller{
function index(){
    $this->load->model('site_model');
    $data['records'] = $this->site_model->getAll();
    $this->load->view('home', $data);
    }}

网站模型.php

<?php
class Site_model extends CI_Model{

  function getAll(){

    $q = $this->db->get('test');
    if($q->num_rows() >0){
    foreach ($q->result() as $row)
    {
     $data[] =$row;
    }
     return $data;
   }
 }
}
?>

home.php 查看页面

<!DOCTYPE>

<html>
<head>
    <title>Site</title>
</head>
<body>
    <div id="container">
        <p>My view has been loaded</p>
    <pre>

<?php print_r($records);?>
    </pre>

    </body>
    </html>

你正在加载数据库吗

$this->load->database();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

出现 null 错误时调用成员函数 get() 的相关文章

随机推荐