lzyatchina 发表于 2012-2-25 01:49:27

$this->load->database();执行不了

本帖最后由 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/则什么也显示不了,是个空白页。

小_宝 发表于 2012-2-25 06:37:20

$active_group = 'default';
$active_record = TRUE;
1.在数据库里将$active_group = 'test';
3.下面的配置都改成:
$db[''test']['hostname'] = 'localhost';的样子
3.加载数据库连接的时候这样写:$this->load->database(‘test’);
试试

小_宝 发表于 2012-2-25 06:40:57

自动连接

“自动连接” 功能将在每个一页面加载时被自动实例化数据库类。要启用“自动连接”,可在application/config/autoload.php中的 library 数组里添加 database:
$autoload['libraries'] = array('database');

lzyatchina 发表于 2012-2-25 20:55:04

谢谢您的帮助,可是问题还是没有解决,我打开的CI的log,如果了“$this->load->database('test');”,则log是这样的:
DEBUG - 2012-02-25 12:48:52 --> Config Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Hooks Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Utf8 Class Initialized
DEBUG - 2012-02-25 12:48:52 --> UTF-8 Support Enabled
DEBUG - 2012-02-25 12:48:52 --> URI Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Router Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Output Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Security Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Input Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Global POST and COOKIE data sanitized
DEBUG - 2012-02-25 12:48:52 --> Language Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Loader Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Controller Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Model Class Initialized
DEBUG - 2012-02-25 12:48:52 --> Database Driver Class Initialized
然后就没有了。

如果把$this->load->database('test');注释掉,则log是这样的:

DEBUG - 2012-02-25 12:50:06 --> Config Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Hooks Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Utf8 Class Initialized
DEBUG - 2012-02-25 12:50:06 --> UTF-8 Support Enabled
DEBUG - 2012-02-25 12:50:06 --> URI Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Router Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Output Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Security Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Input Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Global POST and COOKIE data sanitized
DEBUG - 2012-02-25 12:50:06 --> Language Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Loader Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Controller Class Initialized
DEBUG - 2012-02-25 12:50:06 --> Model Class Initialized
DEBUG - 2012-02-25 12:50:06 --> File loaded: application/views/templates/header.php
DEBUG - 2012-02-25 12:50:06 --> Final output sent to browser
DEBUG - 2012-02-25 12:50:06 --> Total execution time: 0.0988
这样的log就完整了,真奇怪,在phpmyadmin里能登录数据库,能打开表,能插入数据,也能读到数据,在CI里怎么就这么怪了呢?不知道是哪里出错了。

会不会是apache里有什么配置我没注意到弄错了?

lzyatchina 发表于 2012-2-25 21:00:27

小_宝 发表于 2012-2-25 06:37 static/image/common/back.gif
$active_group = 'default';
$active_record = TRUE;
1.在数据库里将$active_group = 'test';


先谢谢你,可是问题还没有解决:
http://www.codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=11798&page=1#pid56153

lzyatchina 发表于 2012-2-25 22:17:49

问题已经解决了。
在php配置文件中我原来只用了php_mysqli.dll,就会出这个问题,只要再用了php_mysql.dll,数据库连接就正常了,这是为什么呢?

309090518 发表于 2012-2-25 22:47:51

两个不同的数据库。

Hex 发表于 2012-2-26 00:38:15

lzyatchina 发表于 2012-2-25 22:17 static/image/common/back.gif
问题已经解决了。
在php配置文件中我原来只用了php_mysqli.dll,就会出这个问题,只要再用了php_mysql.dll ...

这是老问题了,空白页多数是由于没安装某扩展造成的。
你没安装 php_mysql.dll 就没有 mysql_* 这些函数,没函数就报错,但是 CI 屏蔽了错误,所以就是空白页。

lzyatchina 发表于 2012-2-26 10:52:28

Hex 发表于 2012-2-26 00:38 static/image/common/back.gif
这是老问题了,空白页多数是由于没安装某扩展造成的。
你没安装 php_mysql.dll 就没有 mysql_* 这些函数 ...

不知道CI是否能通过设置不屏蔽错误?这样我们新手在学习时能较快的定位到问题?谢谢!

Hex 发表于 2012-2-27 11:22:52

lzyatchina 发表于 2012-2-26 10:52 static/image/common/back.gif
不知道CI是否能通过设置不屏蔽错误?这样我们新手在学习时能较快的定位到问题?谢谢! ...

不可以。因为CI很多函数调用都是加了@的,加了@的函数调用不会显示任何错误。
页: [1] 2
查看完整版本: $this->load->database();执行不了