【发布时间】:2016-06-24 15:59:01
【问题描述】:
我认为无法连接到数据库,我不知道为什么。
我查看了 stackoverflow 并发现了以下问题: here 并没有帮助我解决我的问题。
我已阅读 Codeigniter 3 的文档:here 并使用了选项手动连接。
我的应用程序控制器中的类如下所示:
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
我收到此错误:
致命错误:在 null 上调用成员函数 database() \www\task\application\controllers\home.php 在第 12 行
我试过 var_dump($this->load) ,它是一个空值,从这里我假设它无法建立到数据库的连接。
【问题讨论】:
标签: php codeigniter