CodeIgniter框架數(shù)據(jù)庫基本操作示例
本文實例講述了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)建一個數(shù)據(jù)庫和一個user表,這個在我的源碼包里面有你可以直接導(dǎo)入就好了,但是前提你要創(chuàng)建一個demo的數(shù)據(jù)庫
reg類代碼如下
<?php
/***************************************
* 用戶注冊模塊和數(shù)據(jù)庫的基本操作實踐
* 17:44 2013.2.18
* @Author sdeep wang
***************************************/
class Reg extends CI_Controller{
function __construct(){//此函數(shù)每次必須寫是繼承父類的方法
parent::__construct();
$this->load->database();//這個是連接數(shù)據(jù)庫的方法,放到這里的好處只要調(diào)用該方法就會連接數(shù)據(jù)庫
}
function index(){
$this->load->view('reg_view');//這個是使用哪個視圖來顯示相當(dāng)于Smarty中的display
}
function reg_insert(){
$data['name'] = $this->input->post('name');//這個是指取得POST數(shù)組的值然后賦值一個心的數(shù)組
$data['sex'] = $this->input->post('sex');
$data['age'] = $this->input->post('age');
$data['pwd'] = md5($this->input->post('pwd'));//這里用了一個md5加密只是為了演示
$data['email'] = $this->input->post('email');
$this->db->insert('user',$data);//這個是數(shù)據(jù)庫操作插入操作
redirect('/reg/reg_select/', 'refresh');//這個是跳轉(zhuǎn)函數(shù)是url輔助函數(shù)里面的一個方法
}
function reg_select(){//這個查詢數(shù)據(jù)庫的方法
$this->db->select('id,name,sex,age,email');//這里是查詢要顯示的字段,可不能像我第一次這樣寫啊$this->db->select('id','name','sex','age','email');
$data['query'] = $this->db->get('user');//這個是取得數(shù)據(jù)(如果你上面寫的和我第一次一樣的話只能取的一個字段)
$this->load->view('select_view',$data);//這里是調(diào)用哪個視圖并分配數(shù)據(jù)給指定視圖顯示
}
function reg_delete(){//刪除數(shù)據(jù)的操作
$id = $this->input->get('id');//這里是取得get傳過來的值
$this->db->where('id',$id);//這里是做where條件這個相當(dāng)重要,如果沒有這個你有可能把這個表數(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(){//這個是真正的跟新數(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>用戶注冊</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="注冊" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
第二個視圖代碼如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>顯示數(shù)據(jù)庫中的所有注冊用戶</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>注冊用戶的顯示</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>
第三個視圖如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>修改用戶注冊信息</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>
效果圖如下

就這樣其中里面什么驗證啊,校對之類的都沒有做只是練習(xí)數(shù)據(jù)庫的基本操作。
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。
- codeigniter自帶數(shù)據(jù)庫類使用方法說明
- 讓CodeIgniter數(shù)據(jù)庫緩存自動過期的處理的方法
- 新浪SAE云平臺下使用codeigniter的數(shù)據(jù)庫配置
- codeigniter數(shù)據(jù)庫操作函數(shù)匯總
- Codeigniter操作數(shù)據(jù)庫表的優(yōu)化寫法總結(jié)
- CodeIgniter針對數(shù)據(jù)庫的連接、配置及使用方法
- CodeIgniter框架數(shù)據(jù)庫事務(wù)處理的設(shè)計缺陷和解決方案
- CI框架(CodeIgniter)實現(xiàn)的數(shù)據(jù)庫增刪改查操作總結(jié)
- CI(CodeIgniter)框架配置
- CodeIgniter基本配置詳細介紹
- php框架CodeIgniter主從數(shù)據(jù)庫配置方法分析
相關(guān)文章
Yii2創(chuàng)建控制器(createController)方法詳解
這篇文章主要介紹了Yii2創(chuàng)建控制器(createController)的方法,結(jié)合實例形式分析了Yii創(chuàng)建控制器所使用到的方法、操作步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07
ThinkPHP框架實現(xiàn)session跨域問題的解決方法
這篇文章主要介紹了ThinkPHP框架實現(xiàn)session跨域問題的解決方法,需要的朋友可以參考下2014-07-07
CI框架入門示例之?dāng)?shù)據(jù)庫取數(shù)據(jù)完整實現(xiàn)方法
這篇文章主要介紹了CI框架入門示例的數(shù)據(jù)庫取數(shù)據(jù)完整實現(xiàn)方法,包含了配置、建表與實現(xiàn)MVC的完整過程,需要的朋友可以參考下2014-11-11
PHPStorm 2020.1 調(diào)試 Nodejs的多種方法詳解
這篇文章主要介紹了PHPSTORM 2020.1 調(diào)試 Nodejs的多種方法詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

