调用成员函数get()on null时出错 [英] Call to a member function get() on null error

查看:169
本文介绍了调用成员函数get()on null时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CodeIgniter-2.2.1中得到这个错误(致命错误:调用成员函数get()在C:\ xampp \htdocs\ci\application\models\site_model .php on line 6)我不知道为什么会发生这种情况。我是否正确调用函数?第6行是$ this-> load-> model('site_model');



控制器site.php

 <?php 
类网站扩展CI_Controller {
function index(){
$ this-> load-> model('site_model' );
$ data ['records'] = $ this-> site_model-> getAll();
$ this-> load-> view('home',$ data);
}}

site_model.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>网站< / title>
< / head>
< body>
< div id =container>
< p>我的视图已加载< / p>
< pre>

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

< / body>
< / html>


解决方案

正在加载数据库

  $ this-> load-> database(); 


I keep getting this error in CodeIgniter-2.2.1.(Fatal error: Call to a member function get() on null in C:\xampp\htdocs\ci\application\models\site_model.php on line 6) I am not sure why this is happening. Am I calling the function correctly? Line 6 is $this->load->model('site_model');

controller site.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);
    }}

site_model.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 The view page

<!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>

解决方案

Are loading the database

$this->load->database();

这篇关于调用成员函数get()on null时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆