最簡單的CI架構入門樣本–資料庫取資料

來源:互聯網
上載者:User

這個寫給初學者看,這是最簡單可以調通的例子,網上很多例子其實初學者本地跑不通,缺這少那。

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.建表

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 ;

自己隨便填幾條資料

 

4.實現MVC

1)實現M--取資料

CI的models下建立一個檔案mtest.php

<?phpclass 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

<?phpclass 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

 

<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;");//在mysql_select_db("");後加入.  

//防止中文亂碼 要看你的資料庫字元集

CI  config下的database.php檔案

$db['default']['char_set'] = 'utf8';  //utf8.  資料庫字元集也是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

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.