|
本帖最后由 lzyatchina 于 2012-2-25 01:50 编辑
各位请教了,我是初学者,正在按教程学习,但在按本站教程http://codeigniter.org.cn/user_guide/tutorial/news_section.html 《读取新闻条目》时,页面不显示任何内容,在排查后发现只注释掉$this->load->database();就可以正常(当然也注释掉了get_news相关语句),但只要把$this->load->database();注释去掉页面就什么也不显示。
我是在phpmyadmin中建立了ci_study数据库,并在该数据库中建立了news数据表。
database.php内容如下:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'AkkkJnnn';
$db['default']['database'] = 'ci_study';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
数据库用户名与密码肯定是对的,我就是用这个用户名和密码登录phpmyadmin的。
我在这个论坛里搜索了但似乎没有人碰到这个问题,我是哪里写错了呢?
application\models\news_model.php
<?php
class News_model extends CI_Model {
public function __construct()
{
//$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug == FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
?>
application\controllers\news.php
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
// $data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
// $this->load->view('news/index', $data);
// $this->load->view('templates/footer');
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
?>
像上面这样至少能显示header,但如果把 $this->load->database();注释去掉,访问 http://127.0.0.1/index.php/news/则什么也显示不了,是个空白页。
|
|