天天看點

CI架構的資料庫操作函數

//舉例查詢 (userid,name,sex為user表的資料庫字段)

    public function testa(){

        $this->load->database();

        $query = $this->db->query('SELECT * FROM user');

        foreach ($query->result() as $row)

        {

            echo $row->userid;

            echo $row->name;

            echo $row->sex;

        }

    }

    //舉例插入(userid,name,sex為user表的資料庫字段)

    public function testb(){

    $this->load->database();

    $data = array('userid' => '22','name' => 'lisi',  'sex' => '1');

    echo $str = $this->db->insert('user', $data);

    //舉例更新(userid,name,sex為user表的資料庫字段)

    public function testc(){

        $data = array('userid' => '22','name' => 'lisi',  'sex' => '1');

        $this ->db->where('userid',1);

        $this->db->update('user', $data);

    //舉例删除(userid,name,sex 為)

    public function testd(){

        $this ->db->where('userid',22);

        $this->db->delete('user', $data);

//CI視圖加載顯示變量

public function index()

    {

        $this->load->library('parser');

        $data = array(

                  'blog_title'   => 'My Blog Title',

                  'blog_heading' => 'My Blog Heading',

                  'blog_entries' => array(

                                          array('title' => 'Title 1', 'body' => 'Body 1'),

                                          array('title' => 'Title 2', 'body' => 'Body 2'),

                                          array('title' => 'Title 3', 'body' => 'Body 3'),

                                          array('title' => 'Title 4', 'body' => 'Body 4'),

                                          array('title' => 'Title 5', 'body' => 'Body 5')

                                          )

                );

        $this->parser->parse('welcome_message', $data);

        $this->load->view('welcome_message');

welcome_message.php

<html>

<head>

    <title>{blog_title}</title>

</head>

<body>

    {blog_entries}

</body>

</html>

注意:

在使用<? echo base_url();?> 之前

先設定

config/autoload.php 中的

$autoload['helper'] = array('url', 'form');