gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:12:02 25 4
gpt4 key购买 nike

我在 CodeIgniter-2.2.1 中不断收到此错误。( fatal error :在第 6 行的 C:\xampp\htdocs\ci\application\models\site_model.php 中调用成员函数 get() on null)我不确定为什么会这样。我是否正确调用了函数?第 6 行是 $this->load->model('site_model');

Controller 站点.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();

关于php - 在出现 null 错误时调用成员函数 get(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28230695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com