codeigniter(CI)是一个优秀、敏捷的PHP开源框架,尤其封装了对数据库的操作,很方便,以下是phpci常用的数据库操作,作个记录:
002 | ================================== |
004 | $query =$this->db_query("SELECT*FROMtable"); |
005 | ================================== |
009 | $data =$query->result(); |
012 | $data =$query->result_array(); |
018 | $data =$query->num_rows(); |
020 | //num_fields()
返回查询请求的字段个数 |
021 | $data =$query->num_fields(); |
024 | $data =$query->row_array(); |
026 | //free_result()
释放当前查询所占用的内存并删除关联资源标识 |
027 | $data =$query->free_result(); |
030 | ================================== |
032 | ================================== |
036 | echo
$ this ->db->insert_id(); |
039 | echo
$ this ->db->affected_rows(); |
042 | echo
$ this ->db->count_all( 'table_name' ); |
045 | echo
$ this ->db->version(); |
048 | echo
$ this ->db->platform(); |
051 | echo
$ this ->db->last_query(); |
053 | //插入数据,被插入的数据会被自动转换和过滤,例如: |
054 | //$data =array('name'=>$name,'email'=>$email,'url'=>$url); |
055 | $ this ->db->insert_string( 'table_name' ,
$data); |
058 | ================================== |
060 | ================================== |
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); |
069 | ================================== |
071 | ================================== |
075 | $ this ->db-> get ( 'table_name' ); |
077 | //第二个参数为输出条数,第三个参数为开始位置 |
078 | $ this ->db-> get ( 'table_name' , 10,20); |
080 | //获取数据,第一个参数为表名,第二个为获取条件,第三个为条数 |
081 | $ this ->db->get_where( 'table_name' ,
array( 'id' =>$id),
$offset); |
084 | $ this ->db->select( 'title, content,date' ); |
085 | $data =$ this ->db-> get ( 'table_name' ); |
087 | //获取字段的最大值,第二个参数为别名,相当于max(age) ASnianling |
088 | $ this ->db->select_max( 'age' ); |
089 | $ this ->db->select_max( 'age' , 'nianling' ); |
092 | $ this ->db->select_min( 'age' ); |
093 | $ this ->db->select_min( 'age' , 'nianling' ); |
096 | $ this ->db->select_sum( 'age' ); |
097 | $ this ->db->select_sum( 'age' , 'nianling' ); |
100 | $ this ->db->select( 'title' , content,date'); |
101 | $ this ->db->from( 'table_name' ); |
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); |
109 | $ this ->db->where_in( 'item1' , 'item2' ); |
110 | $ this ->db->where_not_in( 'item1' , 'item2' ); |
112 | //匹配,第三个参数为匹配模式 titleLIKE'%match%' |
113 | $ this ->db->like( 'title' , 'match' , 'before/after/both' ); |
114 | $ this ->db->not_like(); |
117 | $ this ->db->group_by( 'title' , 'date' ); |
120 | $ this ->db->limit(0,
20); |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理