CodeIgniter框架數(shù)據(jù)庫基本操作示例
本文實(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ì)有所幫助。
- codeigniter自帶數(shù)據(jù)庫類使用方法說明
- 讓CodeIgniter數(shù)據(jù)庫緩存自動(dòng)過期的處理的方法
- 新浪SAE云平臺(tái)下使用codeigniter的數(shù)據(jù)庫配置
- codeigniter數(shù)據(jù)庫操作函數(shù)匯總
- Codeigniter操作數(shù)據(jù)庫表的優(yōu)化寫法總結(jié)
- CodeIgniter針對(duì)數(shù)據(jù)庫的連接、配置及使用方法
- CodeIgniter框架數(shù)據(jù)庫事務(wù)處理的設(shè)計(jì)缺陷和解決方案
- CI框架(CodeIgniter)實(shí)現(xiàn)的數(shù)據(jù)庫增刪改查操作總結(jié)
- CI(CodeIgniter)框架配置
- CodeIgniter基本配置詳細(xì)介紹
- php框架CodeIgniter主從數(shù)據(jù)庫配置方法分析
相關(guān)文章
ThinkPHP實(shí)現(xiàn)事務(wù)回滾示例代碼
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)事務(wù)回滾,需要的朋友可以參考下2014-06-06TP5框架model常見操作示例小結(jié)【增刪改查、聚合、時(shí)間戳、軟刪除等】
這篇文章主要介紹了TP5框架model常見操作,結(jié)合實(shí)例形式總結(jié)分析了thinkPHP5框架增刪改查、聚合、獲取器、修改器、時(shí)間戳、軟刪除等常見操作技巧,需要的朋友可以參考下2020-04-04Laravel5.1 框架模型查詢作用域定義與用法實(shí)例分析
這篇文章主要介紹了Laravel5.1 框架模型查詢作用域定義與用法,結(jié)合實(shí)例形式分析了laravel5.1定義一個(gè)查詢作用域及動(dòng)態(tài)的查詢作用域相關(guān)操作技巧,需要的朋友可以參考下2020-01-01php實(shí)現(xiàn)QQ小程序發(fā)送模板消息功能
QQ小程序群里有伙伴要發(fā)送模板消息的代碼,所以今天給大家分享QQ小程序模板消息發(fā)布,絕對(duì)一步一步帶著大家走,每個(gè)細(xì)節(jié)都講到,感興趣的朋友跟隨小編一起看看吧2019-09-09thinkPHP3.2.3結(jié)合Laypage實(shí)現(xiàn)的分頁功能示例
這篇文章主要介紹了thinkPHP3.2.3結(jié)合Laypage實(shí)現(xiàn)的分頁功能,結(jié)合實(shí)例形式分析了thinkPHP3.2.3結(jié)合Laypage實(shí)現(xiàn)分頁的model控制器與view視圖相關(guān)操作技巧,需要的朋友可以參考下2018-05-05NativePHP使用PHP創(chuàng)建桌面應(yīng)用程序
這篇文章主要為大家介紹了NativePHP使用PHP創(chuàng)建桌面應(yīng)用程序,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12