这个写给初学者看,这是最简单可以调通的例子,网上很多例子其实初学者本地跑不通,缺这少那。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";  
 
3.建表
- <strong>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 ;  
- </strong>  
自己随便填几条数据
 
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();  
-     }  
-       
- }  
- ?>  
说明:
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->model('mtest');  
-     $data['query1'] = $this->mtest->get_last_ten_entries();  
-     $this->load->view('users',$data);  
-       
-       
-       
- }  
- }  
- ?>  
调用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
 
- <head>  
-   
- <title><? echo $title;?></title>  
- </head>  
- <body>  
- <ul>  
- <?php foreach($todo_list as $item):?>  
-   
- <li><?php echo $item;?></li>  
-   
- <?php endforeach;?>  
- </ul>  
- <ul>  
- <? echo count($query1);  
- foreach ($query1 as $v1) {  
-     foreach ($v1 as $v2) {  
-         echo "$v2\n";  
-     }  
- }  
-   
- for ($row=0;$row<count($query1);$row++) {  
-     echo $query1[$row]->name."</br>";  
- }  
- ?>  
-   
-   
- <?php foreach($query1 as $v):?>  
-   
- <li><?php echo $v->name;?></li>  
-   
- <?php endforeach;?>  
- </ul>  
- </h2><?php echo $headline; ?></h2>  
-    
- </body>  
- </html>  
说明:可以用For和Foreach多种方法找出你要的数据!
说明:如果是整个页面乱码,网页头部大概是这样的.
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />  
如果你没有使用CI连接数据库,在数据库连接部分加入下面的代码.
mysql_query("SET NAMES GBK"); //防止中文乱码
mysql_query("set names utf8;");  
//防止中文乱码 要看你的数据库字符集
CI  config下的database.php文件
- $db['default']['char_set'] = 'utf8';    
- $db['default']['dbcollat'] = 'utf8_general_ci';  
 
 
更多不明白请参考:
控制器 http://codeigniter.org.cn/user_guide/general/controllers.html
模型      http://codeigniter.org.cn/user_guide/general/models.html
视图      http://codeigniter.org.cn/user_guide/general/views.html
来自  http://blog.csdn.net/21aspnet/article/details/6599780