您的位置:首页 > 数据库

codeigniter对数据库的常用操作

2013-05-10 14:23 423 查看
codeigniter(CI)是一个优秀、敏捷的PHP开源框架,尤其封装了对数据库的操作,很方便,以下是phpci常用的数据库操作,作个记录:

001
/*
002
==================================
003
查询
004
$query
=$this->db_query("SELECT*FROMtable");
005
==================================
006
*/
007
008
//result()
返回对象数组
009
$data
=$query->result();
010
011
//result_array()
返回数据
012
$data
=$query->result_array();
013
014
//row()
只返回一行对象数组
015
$data
=$query->row();
016
017
//num_rows()
返回查询结果行数
018
$data
=$query->num_rows();
019
020
//num_fields()
返回查询请求的字段个数
021
$data
=$query->num_fields();
022
023
//row_array()
只返回一行数组
024
$data
=$query->row_array();
025
026
//free_result()
释放当前查询所占用的内存并删除关联资源标识
027
$data
=$query->free_result();
028
029
/*
030
==================================
031
插入操作
032
==================================
033
*/
034
035
//上次插入操作生成的ID
036
echo
$
this
->db->insert_id();
037
038
//写入和更新操作被影响的行数
039
echo
$
this
->db->affected_rows();
040
041
//返回指定表的总行数
042
echo
$
this
->db->count_all(
'table_name'
);
043
044
//输出当前的数据库版本号
045
echo
$
this
->db->version();
046
047
//输出当前的数据库平台
048
echo
$
this
->db->platform();
049
050
//返回最后运行的查询语句
051
echo
$
this
->db->last_query();
052
053
//插入数据,被插入的数据会被自动转换和过滤,例如:
054
//$data
=array('name'=>$name,'email'=>$email,'url'=>$url);
055
$
this
->db->insert_string(
'table_name'
,
$data);
056
057
/*
058
==================================
059
更新操作
060
==================================
061
*/
062
063
//更新数据,被更新的数据会被自动转换和过滤,例如:
064
//$data
=array('name'=>$name,'email'=>$email,'url'=>$url);
065
//$where
="author_id=1ANDstatus='active'";
066
$
this
->db->update_string(
'table_name'
,
$data,$where);
067
068
/*
069
==================================
070
选择数据
071
==================================
072
*/
073
074
//获取表的全部数据
075
$
this
->db->
get
(
'table_name'
);
076
077
//第二个参数为输出条数,第三个参数为开始位置
078
$
this
->db->
get
(
'table_name'
,
10,20);
079
080
//获取数据,第一个参数为表名,第二个为获取条件,第三个为条数
081
$
this
->db->get_where(
'table_name'
,
array(
'id'
=>$id),
$offset);
082
083
//select方式获取数据
084
$
this
->db->select(
'title,
content,date'
);
085
$data
=$
this
->db->
get
(
'table_name'
);
086
087
//获取字段的最大值,第二个参数为别名,相当于max(age)
ASnianling
088
$
this
->db->select_max(
'age'
);
089
$
this
->db->select_max(
'age'
,
'nianling'
);
090
091
//获取字段的最小值
092
$
this
->db->select_min(
'age'
);
093
$
this
->db->select_min(
'age'
,
'nianling'
);
094
095
//获取字段的和
096
$
this
->db->select_sum(
'age'
);
097
$
this
->db->select_sum(
'age'
,
'nianling'
);
098
099
//自定义from表
100
$
this
->db->select(
'title'
,
content,date');
101
$
this
->db->from(
'table_name'
);
102
103
//查询条件
WHEREname='Joe'ANDtitle='boss'ANDstatus='active'
104
$
this
->db->where(
'name'
,
$name);
105
$
this
->db->where(
'title'
,
$title);
106
$
this
->db->where(
'status'
,
$status);
107
108
//范围查询
109
$
this
->db->where_in(
'item1'
,
'item2'
);
110
$
this
->db->where_not_in(
'item1'
,
'item2'
);
111
112
//匹配,第三个参数为匹配模式
titleLIKE'%match%'
113
$
this
->db->like(
'title'
,
'match'
,
'before/after/both'
);
114
$
this
->db->not_like();
115
116
//分组
GROUPBYtitle,date
117
$
this
->db->group_by(
'title'
,
'date'
);
118
119
//限制条数
120
$
this
->db->limit(0,
20);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: