本文实例讲述了CI框架入门示例之数据库取数据完整实现方法。是写给初学者看的,这是最简单可以调通的例子。分享给大家供大家参考。具体实现方法如下:
1.下载CI框架
2.配置
database.php配置:
为数据库服务器设置 connection 参数:
代码如下:
$db['default']['hostname'] = "your-db-host";
$db['default']['username'] = "your-username";
$db['default']['password'] = "your-password";
$db['default']['database'] = "your-db-name";
$db['default']['dbdriver'] = "mysql";
$db['default']['username'] = "your-username";
$db['default']['password'] = "your-password";
$db['default']['database'] = "your-db-name";
$db['default']['dbdriver'] = "mysql";
3.建表
代码如下:
CREATE TABLE IF NOT EXISTS `users` (
`id` INT(8) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) CHARACTER SET utf8 DEFAULT NULL,
`age` VARCHAR(3) CHARACTER SET utf8 DEFAULT NULL,
`sex` VARCHAR(2) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_estonian_ci AUTO_INCREMENT=14 ;
`id` INT(8) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) CHARACTER SET utf8 DEFAULT NULL,
`age` VARCHAR(3) CHARACTER SET utf8 DEFAULT NULL,
`sex` VARCHAR(2) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_estonian_ci AUTO_INCREMENT=14 ;
自己随便填几条数据
4.实现MVC
1)实现M--取数据
CI的models下新建一个文件mtest.php
代码如下:
<?php
class Mtest extends CI_Model{
function Mtest(){
parent::__construct();
}
function get_last_ten_entries()
{
$this->load->database();
mysql_query("SET NAMES GBK"); //防止中文乱码
$query = $this->db->get('users', 10);
return $query->result();
}
}
?>
class Mtest extends CI_Model{
function Mtest(){
parent::__construct();
}
function get_last_ten_entries()
{
$this->load->database();
mysql_query("SET NAMES GBK"); //防止中文乱码
$query = $this->db->get('users', 10);
return $query->result();
}
}
?>
说明:
parent::__construct();不可少
$this->load->database();一定不能少不然会报错
也可以实现“自动连接” 功能,将在每个一页面加载时被自动实例化数据库类。要启用“自动连接”,可在如下文件中的 library 数组里添加 database:
application/config/autoload.php
不然就要像这里一样写在每个页面上。
也可以用
代码如下:
$query = $this->db->query('select * from users');
这样写入自己的SQL
2)实现C--决定取那些数据
CI的controllers下新建一个文件test.php
代码如下:
<?php
class Test extends CI_Controller {
function Test(){
parent::__construct();
}
function index(){
$this->load->helper('form');
$data['title'] = "首页";
$data['headline'] = "录入用户信息";
//多维数组
$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
//$this->load->vars($data);
$this->load->model('mtest');
$data['query1'] = $this->mtest->get_last_ten_entries();
$this->load->view('users',$data);
//$this->load->view('newfile');
//$this->load->view('a/newfile');
}
}
?>
class Test extends CI_Controller {
function Test(){
parent::__construct();
}
function index(){
$this->load->helper('form');
$data['title'] = "首页";
$data['headline'] = "录入用户信息";
//多维数组
$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
//$this->load->vars($data);
$this->load->model('mtest');
$data['query1'] = $this->mtest->get_last_ten_entries();
$this->load->view('users',$data);
//$this->load->view('newfile');
//$this->load->view('a/newfile');
}
}
?>
调用model:
代码如下:
$this->load->model('mtest');
把model装载到数组里:
代码如下:
$data['query1'] = $this->mtest->get_last_ten_entries();
把数组转载到页面上:
代码如下:
$this->load->view('users',$data);
2)实现V--页面显示
CI的views下新建一个文件user.php
希望本文所述对大家CI框架程序设计的学习有所帮助。