CodeIgniter框架基本增刪改查操作示例
本文實(shí)例講述了CodeIgniter框架基本增刪改查操作。分享給大家供大家參考,具體如下:
對于codeigniter的增刪改,在此我用自己的一個(gè)例子來說明一下:
創(chuàng)建數(shù)據(jù)庫:
CREATE TABLE IF NOT EXISTS `users` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `email` varchar(100) NOT NULL, `fullname` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `email` (`email`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Model
MTest.php
<?php
class MTest extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
//insert into data
public function insert_users($arr){
$this->db->insert('users',$arr);
}
//delete data
public function delete_users($id){
$this->db->where('id',$id);
$this->db->delete('users');
}
//Alter data
public function update_users($username,$arr){
$this->db->where('username',$username);
$this->db->update('users',$arr);
}
//select data
public function select_users($id){
$this->db->where('id',$id);
$this->db->select('*');
return $this->db->get('users');
//注意如果你返回的是以下內(nèi)容的話,他就是一個(gè)對象的結(jié)果集,這樣當(dāng)你返回controller中時(shí),你就得轉(zhuǎn)換;
// return $query->result();
}
}
?>
Controller
home.php
<?php
/**
* xxx.php
* ==============================================
* Copy right 2012-2015
* ----------------------------------------------
* This is not a free software, without any authorization is not allowed to use and spread.
* ==============================================
* @Author:YeXianMing
* @Email:LangWaiShiGe@hotmail.com
* @Version:zend studio10.6.2 php5.4.38 apache2.2
*/
if(!defined('BASEPATH'))exit('No direct script access allowed');
class Home extends CI_Controller{
public function __construct(){
parent::__construct();
}
//insert data
public function insert(){
$this->load->model('MTest','',TRUE);
$arr=array('username'=>'yexianming1','password'=>'admin','email'=>'11504160314qq.com','fullname'=>'luotianyexianming');
if(!($this->MTest->insert_users($arr))){
echo "插入數(shù)據(jù)成功";
}else{
echo "插入數(shù)據(jù)失敗";
}
}
//update data
public function update(){
$this->load->model('MTest','',TRUE);
$arr=array('username'=>'Helloworld22','password'=>'root','email'=>'895787704@qq.com','fullname'=>'luotianyecong');
if(!($this->MTest->update_users('Helloworld',$arr))){
echo "修改成功";
}else{
echo "修改失敗";
}
}
//delete data
public function delete(){
$this->load->model('MTest','',TRUE);
$query=$this->MTest->delete_users(12);
if(!$query){
echo "刪除成功";
}else{
echo "刪除失敗";
}
}
//select data
public function select(){
$this->load->library('table');
$this->load->model('MTest','',TRUE);
$arr=$this->MTest->select_users(3);
$userinformation=$this->table->generate($arr);
$data['userinfor']=$userinformation;
$this->load->view('template',$data);
}
}
?>
view
temlate.php
<?php echo $userinfor;?>
output:
id username password email fullname 3 Helloworld22 root 1111111@qq.com luotianyecong
更多關(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ù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
- CI(CodeIgniter)框架中的增刪改查操作
- CodeIgniter刪除和設(shè)置Cookie的方法
- CI框架數(shù)據(jù)庫查詢緩存優(yōu)化的方法
- CI框架數(shù)據(jù)庫查詢之join用法分析
- CI框架AR數(shù)據(jù)庫操作常用函數(shù)總結(jié)
- CI框架出現(xiàn)mysql數(shù)據(jù)庫連接資源無法釋放的解決方法
- CI框架中數(shù)據(jù)庫操作函數(shù)$this->db->where()相關(guān)用法總結(jié)
- CI配置多數(shù)據(jù)庫訪問的方法
- CI框架入門示例之?dāng)?shù)據(jù)庫取數(shù)據(jù)完整實(shí)現(xiàn)方法
- php中CI操作多個(gè)數(shù)據(jù)庫的代碼
- 新浪SAE云平臺下使用codeigniter的數(shù)據(jù)庫配置
- codeigniter數(shù)據(jù)庫操作函數(shù)匯總
- codeigniter自帶數(shù)據(jù)庫類使用方法說明
相關(guān)文章
在laravel框架中實(shí)現(xiàn)封裝公共方法全局調(diào)用
今天小編就為大家分享一篇在laravel框架中實(shí)現(xiàn)封裝公共方法全局調(diào)用,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
laravel 實(shí)現(xiàn)根據(jù)字段不同值做不同查詢
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)根據(jù)字段不同值做不同查詢,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
php版微信開發(fā)之接收消息,自動判斷及回復(fù)相應(yīng)消息的方法
這篇文章主要介紹了php版微信開發(fā)之接收消息,自動判斷及回復(fù)相應(yīng)消息的方法,涉及基于微信消息處理接口的調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2016-09-09
php腳本運(yùn)行時(shí)的超時(shí)機(jī)制詳解
在我們平常的開發(fā)中,也許曾經(jīng)都遇到過PHP腳本運(yùn)行超時(shí)的情況,當(dāng)遇到這種情況我們經(jīng)常會通過使用 set_time_limit(非安全模式),或修改配置文件并重啟服務(wù)器,或者修改程序減少程序的執(zhí)行時(shí)間,使其在允許的范圍之內(nèi),以解決此問題。2016-02-02
php多種形式發(fā)送郵件(mail qmail郵件系統(tǒng) phpmailer類)
這篇文章主要介紹了php多種形式發(fā)送郵件的方法,包括使用mail()函數(shù)、使用管道的形式、使用phpmailer類等方法,大家參考使用吧2014-01-01

