《PHP學習:CodeIgniter框架基本增刪改查操作示例》要點:
本文介紹了PHP學習:CodeIgniter框架基本增刪改查操作示例,希望對您有用。如果有疑問,可以聯系我們。
本文實例講述了CodeIgniter框架基本增刪改查操作.分享給大家供大家參考,具體如下:PHP編程
對于codeigniter的增刪改,在此我用自己的一個例子來說明一下:PHP編程
創建數據庫:PHP編程
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;
ModelPHP編程
MTest.phpPHP編程
<?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'); //注意如果你返回的是以下內容的話,他就是一個對象的結果集,這樣當你返回controller中時,你就得轉換; // return $query->result(); } } ?>
ControllerPHP編程
home.phpPHP編程
<?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 "插入數據成功"; }else{ echo "插入數據失敗"; } } //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); } } ?>
viewPHP編程
temlate.phpPHP編程
<?php echo $userinfor;?>
output:PHP編程
id username password email fullname 3 Helloworld22 root 1111111@qq.com luotianyecong
更多關于CodeIgniter相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優秀開發框架總結》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《Zend FrameWork框架入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》PHP編程
希望本文所述對大家基于CodeIgniter框架的PHP程序設計有所幫助.PHP編程
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/1332.html