gpt4 book ai didi

php - Codeigniter 3 fatal error : Call to a member function database() on null

转载 作者:行者123 更新时间:2023-12-05 09:20:19 24 4
gpt4 key购买 nike

我认为无法连接到数据库,我不知道为什么。

我查看了 stackoverflow 并发现了这个问题: here它并没有帮助我解决我的问题。

我已经阅读了 Codeigniter 3 的文档:here并使用了选项手动连接

我的应用程序 Controller 中的类如下所示:

class home extends CI_Controller {

/**
* Class constructor
* Load database lib
*/
public function __construct()
{
$this->load->database();

}

/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/home.php/welcome
* - or -
* http://example.com/home.php/welcome/index
*/
public function index()
{
$query = $this->db->get('users');

foreach ($query->result() as $row)
{
var_dump($row->fullName); //testing purpose
}

//$this->load->view('home', $data);

}

我的应用程序的数据库配置如下所示:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'user',
'password' => 'password',
'database' => 'tasks',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => FALSE
);

当我访问 http://localhost/home.php/welcome

我收到这个错误:

Fatal error: Call to a member function database() on null in \www\task\application\controllers\home.php on line 12

我尝试了 var_dump($this->load) 并且它是一个 null,从这里我假设它无法建立与数据库的连接。

最佳答案

由于您正在扩展 CI_Controller 类并选择重载 __construct 方法,因此您只需调用父构造即可开始利用 CI 的核心功能。

class home extends CI_Controller
{
public function __construct()
{
// $this->load does not exist until after you call this
parent::__construct(); // Construct CI's core so that you can use it

$this->load->database();
}
}

参见 http://www.codeigniter.com/user_guide/general/controllers.html#class-constructors了解更多详情。

关于php - Codeigniter 3 fatal error : Call to a member function database() on null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38017533/

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