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

CodeIgniter框架數(shù)據(jù)庫基本操作示例

 更新時(shí)間:2018年05月24日 14:52:37   作者:夏凱  
這篇文章主要介紹了CodeIgniter框架數(shù)據(jù)庫基本操作,結(jié)合實(shí)例形式分析了CodeIgniter框架針對(duì)mysql數(shù)據(jù)庫的配置、用戶注冊(cè)、信息查詢、修改及刪除等基本操作技巧,需要的朋友可以參考下

本文實(shí)例講述了CodeIgniter框架數(shù)據(jù)庫基本操作。分享給大家供大家參考,具體如下:

現(xiàn)在開始,首先現(xiàn)在CI框架到自己的服務(wù)器目錄下并配置config/config.php

$config['base_url'] = 'http://localhost:90/CI/';

接著下來配置數(shù)據(jù)庫在config/databases.php我做練習(xí)配置如下

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'demo';
$db['default']['dbdriver'] = 'mysql';

別的現(xiàn)在新手用不到緊接著創(chuàng)建一個(gè)數(shù)據(jù)庫和一個(gè)user表,這個(gè)在我的源碼包里面有你可以直接導(dǎo)入就好了,但是前提你要?jiǎng)?chuàng)建一個(gè)demo的數(shù)據(jù)庫

reg類代碼如下

<?php
/***************************************
 * 用戶注冊(cè)模塊和數(shù)據(jù)庫的基本操作實(shí)踐
 * 17:44 2013.2.18
 * @Author sdeep wang
 ***************************************/
class Reg extends CI_Controller{
  function __construct(){//此函數(shù)每次必須寫是繼承父類的方法
    parent::__construct();
    $this->load->database();//這個(gè)是連接數(shù)據(jù)庫的方法,放到這里的好處只要調(diào)用該方法就會(huì)連接數(shù)據(jù)庫
  }
  function index(){
    $this->load->view('reg_view');//這個(gè)是使用哪個(gè)視圖來顯示相當(dāng)于Smarty中的display
  }
  function reg_insert(){
    $data['name'] = $this->input->post('name');//這個(gè)是指取得POST數(shù)組的值然后賦值一個(gè)心的數(shù)組
    $data['sex'] = $this->input->post('sex');
    $data['age'] = $this->input->post('age');
    $data['pwd'] = md5($this->input->post('pwd'));//這里用了一個(gè)md5加密只是為了演示
    $data['email'] = $this->input->post('email');
    $this->db->insert('user',$data);//這個(gè)是數(shù)據(jù)庫操作插入操作
    redirect('/reg/reg_select/', 'refresh');//這個(gè)是跳轉(zhuǎn)函數(shù)是url輔助函數(shù)里面的一個(gè)方法
  }
  function reg_select(){//這個(gè)查詢數(shù)據(jù)庫的方法
    $this->db->select('id,name,sex,age,email');//這里是查詢要顯示的字段,可不能像我第一次這樣寫啊$this->db->select('id','name','sex','age','email');
    $data['query'] = $this->db->get('user');//這個(gè)是取得數(shù)據(jù)(如果你上面寫的和我第一次一樣的話只能取的一個(gè)字段)
    $this->load->view('select_view',$data);//這里是調(diào)用哪個(gè)視圖并分配數(shù)據(jù)給指定視圖顯示
  }
  function reg_delete(){//刪除數(shù)據(jù)的操作
    $id = $this->input->get('id');//這里是取得get傳過來的值
    $this->db->where('id',$id);//這里是做where條件這個(gè)相當(dāng)重要,如果沒有這個(gè)你有可能把這個(gè)表數(shù)據(jù)都清空了
    $this->db->delete('user');//刪除指定id數(shù)據(jù)
    redirect('/reg/reg_select/', 'refresh');//同上跳轉(zhuǎn)
  }
  function reg_update(){//跟新數(shù)據(jù)的操作
    $data['id'] = $this->input->get('id');//同上取的get傳值過來的ID
    $this->load->view('update_view',$data);//同上調(diào)用視圖分配數(shù)據(jù)
  }
  function reg_com_update(){//這個(gè)是真正的跟新數(shù)據(jù)操作方法
    $id = $this->input->post('id');//同上取得post中的id值
    $data = array(//把post數(shù)組的值封裝到新的數(shù)組中為了下面跟新操作用
          'name'=>$this->input->post('name'),
          'pwd'=>md5($this->input->post('pwd')),
          'email'=>$this->input->post('email' )
        );
    if(!empty($id) && (count($data) > 1)){//判斷id值是否傳過來并且判斷封裝的數(shù)組是否有元素存在
      $this->db->where('id',$id);//同上準(zhǔn)備where條件
      $this->db->update('user',$data);//跟新操作
    }
    redirect('/reg/reg_select/', 'refresh');//同上跳轉(zhuǎn)
  }
}
?>

視圖代碼如下

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>用戶注冊(cè)</title>
  </head>
  <body>
    <form action="<?php echo site_url('reg/reg_insert/'); ?>" method="post">
      <table>
        <tr>
          <td>
            姓名:<input type="text" name="name" />
          </td>
        </tr>
        <tr>
          <td>
            姓別:<input type="radio" name="sex" value="1" />男
               <input type="radio" name="sex" />女
          </td>
        </tr>
        <tr>
          <td>
            年齡:<input type="text" name="age" />
          </td>
        </tr>
        <tr>
          <td>
            密碼:<input type="password" name="pwd" />
          </td>
        </tr>
        <tr>
          <td>
            郵件:<input type="text" name="email" />
          </td>
        </tr>
        <tr>
          <td>
            <input type="submit" value="注冊(cè)" />
            <input type="reset" value="重置" />
          </td>
        </tr>
      </table>
    </form>
  </body>
</html>

第二個(gè)視圖代碼如下

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>顯示數(shù)據(jù)庫中的所有注冊(cè)用戶</title>
    <style>
      *{
        margin:0 auto;
      }
      table {
        border:1px solid gray;
        border-collapse: collapse;
        width:500px;
        text-align:center;
      }
      th,td {
        border:1px solid gray;
      }
    </style>
  </head>
  <body>
    <table>
      <caption><h3>注冊(cè)用戶的顯示</h3></caption>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Sex</th>
        <th>Age</th>
        <th>Email</th>
        <th>Operate</th>
      </tr>
      <?php foreach($query->result() as $item):?>
      <tr>
        <td><?php echo $item->id; ?></td>
        <td><?php echo $item->name; ?></td>
        <td><?php echo $item->sex; ?></td>
        <td><?php echo $item->age; ?></td>
        <td><?php echo $item->email; ?></td>
        <td>
          <a href="<?php echo site_url('reg/reg_delete');?>?id=<?php echo $item->id;?>" rel="external nofollow" >刪除</a> |
          <a href="<?php echo site_url('reg/reg_update');?>?id=<?php echo $item->id;?>" rel="external nofollow" >修改</a>
        </td>
      </tr>
      <?php endforeach; ?>
    </table>
  </body>
</html>

第三個(gè)視圖如下

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>修改用戶注冊(cè)信息</title>
  </head>
  <body>
    <form action="<?php echo site_url('reg/reg_com_update');?>" method="post">
      <table>
        <tr>
          <td>姓名:<input type="text" name="name" /></td>
        </tr>
        <tr>
          <td>密碼:<input type="password" name="pwd" /></td>
        </tr>
        <tr>
          <td>郵件:<input type="text" name="email" /></td>
        </tr>
        <tr>
          <td>
            <input type="submit" value="修改" />
            <input type="hidden" name="id" value="<?php echo $id; ?>" />
          </td>
        </tr>
      </table>
    </form>
  </body>
</html>

效果圖如下

就這樣其中里面什么驗(yàn)證啊,校對(duì)之類的都沒有做只是練習(xí)數(shù)據(jù)庫的基本操作。

更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論