本文实例讲述了CodeIgniter框架基本增删改查操作。分享给大家供大家参考,具体如下:
对于codeigniter的增删改,在此我用自己的一个例子来说明一下:
创建数据库:
1
2
3
4
5
6
7
8
9
10
|
CREATETABLEIFNOTEXISTS `users` (
`id`int(10)NOTNULLAUTO_INCREMENT,
`username`varchar(50)NOTNULL,
`password`varchar(50)NOTNULL,
`email`varchar(100)NOTNULL,
`fullname`varchar(100)NOTNULL,
PRIMARYKEY(`id`),
UNIQUEKEY`username` (`username`),
UNIQUEKEY`email` (`email`)
) ENGINE=InnoDBDEFAULTCHARSET=utf8;
|
Model
MTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?php
classMTestextendsCI_Model{
publicfunction__construct(){
parent::__construct();
$this->load->database();
}
//insert into data
publicfunctioninsert_users($arr){
$this->db->insert('users',$arr);
}
//delete data
publicfunctiondelete_users($id){
$this->db->where('id',$id);
$this->db->delete('users');
}
//Alter data
publicfunctionupdate_users($username,$arr){
$this->db->where('username',$username);
$this->db->update('users',$arr);
}
//select data
publicfunctionselect_users($id){
$this->db->where('id',$id);
$this->db->select('*');
return$this->db->get('users');
//注意如果你返回的是以下内容的话,他就是一个对象的结果集,这样当你返回controller中时,你就得转换;
// return $query->result();
}
}
?>
|
Controller
home.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?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');
classHomeextendsCI_Controller{
publicfunction__construct(){
parent::__construct();
}
//insert data
publicfunctioninsert(){
$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
publicfunctionupdate(){
$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
publicfunctiondelete(){
$this->load->model('MTest','',TRUE);
$query=$this->MTest->delete_users(12);
if(!$query){
echo"删除成功";
}else{
echo"删除失败";
}
}
//select data
publicfunctionselect(){
$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
1
|
<?phpecho$userinfor;?>
|
output:
1
2
|
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常见数据库操作技巧汇总》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。