欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

CI框架入門示例之?dāng)?shù)據(jù)庫(kù)取數(shù)據(jù)完整實(shí)現(xiàn)方法

 更新時(shí)間:2014年11月05日 14:59:26   投稿:shichen2014  
這篇文章主要介紹了CI框架入門示例的數(shù)據(jù)庫(kù)取數(shù)據(jù)完整實(shí)現(xiàn)方法,包含了配置、建表與實(shí)現(xiàn)MVC的完整過(guò)程,需要的朋友可以參考下

本文實(shí)例講述了CI框架入門示例之?dāng)?shù)據(jù)庫(kù)取數(shù)據(jù)完整實(shí)現(xiàn)方法。是寫(xiě)給初學(xué)者看的,這是最簡(jiǎn)單可以調(diào)通的例子。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

1.下載CI框架

2.配置

database.php配置:

為數(shù)據(jù)庫(kù)服務(wù)器設(shè)置 connection 參數(shù):

復(fù)制代碼 代碼如下:
$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.建表

復(fù)制代碼 代碼如下:
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 ;

自己隨便填幾條數(shù)據(jù)

4.實(shí)現(xiàn)MVC
1)實(shí)現(xiàn)M--取數(shù)據(jù)
CI的models下新建一個(gè)文件mtest.php

復(fù)制代碼 代碼如下:
<?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(); 
    } 

?>

說(shuō)明:

parent::__construct();不可少
$this->load->database();一定不能少不然會(huì)報(bào)錯(cuò)
也可以實(shí)現(xiàn)“自動(dòng)連接” 功能,將在每個(gè)一頁(yè)面加載時(shí)被自動(dòng)實(shí)例化數(shù)據(jù)庫(kù)類。要啟用“自動(dòng)連接”,可在如下文件中的 library 數(shù)組里添加 database:
application/config/autoload.php
不然就要像這里一樣寫(xiě)在每個(gè)頁(yè)面上。
也可以用

復(fù)制代碼 代碼如下:
$query = $this->db->query('select * from users');

這樣寫(xiě)入自己的SQL
 
2)實(shí)現(xiàn)C--決定取那些數(shù)據(jù)
CI的controllers下新建一個(gè)文件test.php
復(fù)制代碼 代碼如下:
<?php 
class Test extends CI_Controller { 
  function Test(){ 
    parent::__construct(); 
  } 
  function index(){ 
    $this->load->helper('form'); 
    $data['title'] = "首頁(yè)"; 
    $data['headline'] = "錄入用戶信息"; 
    //多維數(shù)組 
    $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'); 


?>

調(diào)用model:
復(fù)制代碼 代碼如下:
$this->load->model('mtest');

把model裝載到數(shù)組里:
復(fù)制代碼 代碼如下:
$data['query1'] = $this->mtest->get_last_ten_entries();

把數(shù)組轉(zhuǎn)載到頁(yè)面上:
復(fù)制代碼 代碼如下:
$this->load->view('users',$data);

2)實(shí)現(xiàn)V--頁(yè)面顯示
CI的views下新建一個(gè)文件user.php

復(fù)制代碼 代碼如下:
<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>

說(shuō)明:可以用For和Foreach多種方法找出你要的數(shù)據(jù)!
說(shuō)明:如果是整個(gè)頁(yè)面亂碼,網(wǎng)頁(yè)頭部大概是這樣的.
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

如果你沒(méi)有使用CI連接數(shù)據(jù)庫(kù),在數(shù)據(jù)庫(kù)連接部分加入下面的代碼.
復(fù)制代碼 代碼如下:
mysql_query("SET NAMES GBK"); //防止中文亂碼
mysql_query("set names utf8;");//在mysql_select_db("");后加入. 
//防止中文亂碼 要看你的數(shù)據(jù)庫(kù)字符集

CI  config下的database.php文件
復(fù)制代碼 代碼如下:
$db['default']['char_set'] = 'utf8';  //utf8.  數(shù)據(jù)庫(kù)字符集也是utf8 
$db['default']['dbcollat'] = 'utf8_general_ci';

希望本文所述對(duì)大家CI框架程序設(shè)計(jì)的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論