|
各位好,根据教程学习中,首次接触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,由于没有错误可循,我现在不知道该如何处理了,求帮助。
|
|