用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1635|回复: 4
收起左侧

[HELP] codeIgniter连接mysql后页面上没有任何反映

[复制链接]
发表于 2014-10-14 15:38:43 | 显示全部楼层 |阅读模式
按照CI的用户手册教程连接的数据库;读取新闻条目
http://codeigniter.org.cn/user_guide/tutorial/news_section.html


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->getnews();
                $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->getnews($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');
        }
}
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->result_array();
        }

}
header.php文件
<html>
<head>
  <title><?php echo $title ?> - CodeIgniter 2 Tutorial</title>
</head>
<body>
  <h1>CodeIgniter 2 Tutorial</h1>



news/index.php文件
<?php foreach ($news as $news_item): ?>

    <h2><?php echo $news_item['title'] ?></h2>
    <div class="main">
        <?php echo $news_item['text'] ?>
    </div>
    <p><a href="././index.php/news/<?php echo $news_item['slug'] ?>">View article</a></p>

<?php endforeach ?>
footer.php文件
<strong>&copy; 2011</strong>
</body>

</html>
数据库配置文件

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'ci_model';
$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;

用户名,密码,数据库名均正确;
刷新页面没有任何反应,无报错
发表于 2014-10-14 15:47:41 | 显示全部楼层
你的方法名 " get_news "
你呼叫名 " getnews "

當然找不到...
 楼主| 发表于 2014-10-14 15:55:32 | 显示全部楼层
Closer 发表于 2014-10-14 15:47
你的方法名 " get_news "
你呼叫名 " getnews "

我靠,终于找找问题了,谢谢了,我还一直担心配置的问题呢
发表于 2014-10-14 16:02:05 | 显示全部楼层
qirixi 发表于 2014-10-14 15:55
我靠,终于找找问题了,谢谢了,我还一直担心配置的问题呢

沒事沒事,我偶爾也會發生這種事
通常有問題出現,第一首要的就是到處去放

die(print_r($x));  //$x=問題變數

看有沒有抓到值,沒有抓到的話多半是來源出了問題
慢慢追上去總會找到的

加油!
 楼主| 发表于 2014-10-14 16:21:28 | 显示全部楼层
Closer 发表于 2014-10-14 16:02
沒事沒事,我偶爾也會發生這種事
通常有問題出現,第一首要的就是到處去放

十分感谢,原来在CI里普通的echo,die也能直接输出到页面,这样就好调试多了,之前还以为在view以外的输出都输出不出来呢

本版积分规则