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

$this->load->database('test')空白页[老问题,但依旧解决不了]

[复制链接]
发表于 2013-9-3 10:26:13 | 显示全部楼层 |阅读模式
       各位好,根据教程学习中,首次接触PHP语言也是,学习到读取新闻条目的时候,拷贝代码,最后打开页面失败,为空白页,代码调试后发现在model的构造函数中执行了$this->load->database()引起失败,初步判断是数据库连接方面的问题。
       网上查询多方面资料后发现,可能是由于mysql的扩展没加载,导致失败,为判断我的扩展是正常的,首先我使用
phpinfo()函数输出结果,是可以发现mysql和mysqli这两个列表选项的,其次我使用原生态的php操作数据库语句:
PHP复制代码
 
<?php
        $con = mysql_connect("localhost","ben","123456");
        if(!$con){
                die('Could not connect: ' . mysql_error());
        }else{
                echo 'conn success <br />';
               
                $result = mysql_query("SELECT * FROM blog.news");
 
                while($row = mysql_fetch_array($result)){
                        echo $row['title'] . " " . $row['text'];
                        echo "<br />";
                }
        }
?>
 
复制代码

也是可以正常出结果的,所以我怀疑可能是我的CI配置有问题。下面是我的CI部分数据库的配置信息:
$active_group = 'test';
$active_record = TRUE;

$db['test']['hostname'] = 'localhost';
$db['test']['username'] = 'ben';
$db['test']['password'] = '123456';
$db['test']['database'] = 'blog';
$db['test']['dbdriver'] = 'mysql';
$db['test']['dbprefix'] = '';
$db['test']['pconnect'] = TRUE;
$db['test']['db_debug'] = TRUE;
$db['test']['cache_on'] = FALSE;
$db['test']['cachedir'] = '';
$db['test']['char_set'] = 'utf8';
$db['test']['dbcollat'] = 'utf8_general_ci';
$db['test']['swap_pre'] = '';
$db['test']['autoinit'] = TRUE;
$db['test']['stricton'] = FALSE;


这里至所以改成test也是网上部分大侠说的,我不知原理。model的代码如下:
PHP复制代码
 
<?php
class news_model extends CI_Model {
 
  public function __construct(){
    $this->load->database('test');
  }
 
  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();
  }
}
?>
 
复制代码

controller代码如下:
PHP复制代码
 
<?php
class News extends CI_Controller {
 
  public function __construct()
  {
    parent::__construct();
    $this->load->model('news_model');
  }
 
  public function index()
  {
        echo 'in';
   $data['news'] = $this->news_model->get_news();
        echo 'cc';
        $data['title'] = 'News archive';
       
        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
        echo 'end';
  }
 
  public function view($slug)
  {
        echo 'view';
    //$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');
  }
}
?>
 
复制代码



apache的启动信息也没有异常:
[Mon Sep 02 21:21:35 2013] [notice] Apache/2.2.19 (Win64) PHP/5.2.5 (x64) configured -- resuming normal operations
[Mon Sep 02 21:21:35 2013] [notice] Server built: May 28 2011 15:18:56
[Mon Sep 02 21:21:35 2013] [notice] Parent: Created child process 4472
[Mon Sep 02 21:21:35 2013] [notice] Child 4472: Child process is running
[Mon Sep 02 21:21:35 2013] [notice] Child 4472: Acquired the start mutex.
[Mon Sep 02 21:21:35 2013] [notice] Child 4472: Starting 64 worker threads.
[Mon Sep 02 21:21:35 2013] [notice] Child 4472: Starting thread to listen on port 1985.

php.ini里面加了两个扩展:
extension=php_mysql.dll
extension=php_mysqli.dll
关于DLL也全部拷贝到windows下面的system32,path环境变量也加了PHP的路径已经PHP/EXT的路径了


CodeIgniter我用的版本是最新的2.1.4,由于没有错误可循,我现在不知道该如何处理了,求帮助。



发表于 2013-9-3 14:20:05 | 显示全部楼层
你是不是把错误提示关了?
 楼主| 发表于 2013-9-3 16:32:33 | 显示全部楼层
kinwyb 发表于 2013-9-3 14:20
你是不是把错误提示关了?

关于错误提示的话:
在php.in中,已经如下配置:
error_reporting  =  E_ALL & ~E_NOTICE
display_errors=on
在CI框架的index.php中改成了如下:
PHP复制代码
 
/*
if (defined('ENVIRONMENT'))
{
        switch (ENVIRONMENT)
        {
                case 'development':
                        error_reporting(E_ALL);
                break;
       
                case 'testing':
                case 'production':
                        error_reporting(0);
                break;
 
                default:
                        exit('The application environment is not set correctly.');
        }
}
*/

 
error_reporting(E_ALL);
 
复制代码

不知道是否就这样可以了,而且关键看到好多帖子说CI框架用了@的原因导致错误显示不出来啥的,我瞬间又迷茫了。
发表于 2013-9-3 20:44:04 | 显示全部楼层
看看是不是打开了 P_Connect
 楼主| 发表于 2013-9-5 15:56:51 | 显示全部楼层
Hex 发表于 2013-9-3 20:44
看看是不是打开了 P_Connect

这个配置是在哪里的?没有找到额
发表于 2013-9-5 16:34:47 | 显示全部楼层
shaobenbin 发表于 2013-9-5 15:56
这个配置是在哪里的?没有找到额

config/databases.php
 楼主| 发表于 2013-9-6 10:49:31 | 显示全部楼层
Hex 发表于 2013-9-5 16:34
config/databases.php

$db['test']['pconnect'] = FALSE;
已经设置成FALSE了,还是不行,现在我只能再重新下个稍微旧一点的版本重新试一遍了貌似。
 楼主| 发表于 2013-9-6 11:39:32 | 显示全部楼层
重新弄了一遍,仍旧不行,难道这个问题只有我遇到么?奇怪了。
发表于 2013-9-6 13:01:25 | 显示全部楼层
shaobenbin 发表于 2013-9-6 10:49
$db['test']['pconnect'] = FALSE;
已经设置成FALSE了,还是不行,现在我只能再重新下个稍微旧一点的版本 ...

空白页的问题就是因为某些函数执行失败,我遇到99%的情况是某个函数不存在。
实际上就是你的PHP环境有问题,具体你需要看看 php log,如果你有打开 log 的话,或者自己设置断点看看哪个函数执行出错了。

这种确实是个例,正常 PHP 环境是不会出现空白页的。

发表于 2013-11-29 01:53:47 | 显示全部楼层
我也碰到这个问题 , 还没解决,楼主解决没?

本版积分规则