Check out the Latest Articles:
2 ==================================
3 查询
4 $query = $this->db_query("SELECT * FROM table");
5 ==================================
6 */
7
8 //result() 返回对象数组
9 $data = $query->result();
10
11 //result_array() 返回数据
12 $data = $query->result_array();
13
14 //row() 只返回一行对象数组
15 $data = $query->row();
16
17 //num_rows() 返回查询结果行数
18 $data = $query->num_rows();
19
20 //num_fields() 返回查询请求的字段个数
21 $data = $query->num_fields();
22
23 //row_array() 只返回一行数组
24 $data = $query->row_array();
25
26 //free_result() 释放当前查询所占用的内存并删除关联资源标识
27 $data = $query->free_result();
28
29 /*
30 ==================================
31 插入操作
32 ==================================
33 */
34
35 //上次插入操作生成的ID
36 echo $this->db->insert_id();
37
38 //写入和更新操作被影响的行数
39 echo $this->db->affected_rows();
40
41 //返回指定表的总行数
42 echo $this->db->count_all('table_name');
43
44 //输出当前的数据库版本号
45 echo $this->db->version();
46
47 //输出当前的数据库平台
48 echo $this->db->platform();
49
50 //返回最后运行的查询语句
51 echo $this->db->last_query();
52
53 //插入数据,被插入的数据会被自动转换和过滤,例如:
54 //$data = array('name' => $name, 'email' => $email, 'url' => $url);
55 $this->db->insert_string('table_name', $data);
56
57 /*
58 ==================================
59 更新操作
60 ==================================
61 */
62
63 //更新数据,被更新的数据会被自动转换和过滤,例如:
64 //$data = array('name' => $name, 'email' => $email, 'url' => $url);
65 //$where = "author_id = 1 AND status = 'active'";
66 $this->db->update_string('table_name', $data, $where);
67
68 /*
69 ==================================
70 选择数据
71 ==================================
72 */
73
74 //获取表的全部数据
75 $this->db->get('table_name');
76
77 //第二个参数为输出条数,第三个参数为开始位置
78 $this->db->get('table_name', 10, 20);
79
80 //获取数据,第一个参数为表名,第二个为获取条件,第三个为条数
81 $this->db->get_where('table_name', array('id'=>$id), $offset);
82
83 //select方式获取数据
84 $this->db->select('title, content, date');
85 $data = $this->db->get('table_name');
86
87 //获取字段的最大值,第二个参数为别名,相当于max(age) AS nianling
88 $this->db->select_max('age');
89 $this->db->select_max('age', 'nianling');
90
91 //获取字段的最小值
92 $this->db->select_min('age');
93 $this->db->select_min('age', 'nianling');
94
95 //获取字段的和
96 $this->db->select_sum('age');
97 $this->db->select_sum('age', 'nianling');
98
99 //自定义from表
100 $this->db->select('title', content, date');
101 $this->db->from('table_name');
102
103 //查询条件 WHERE name = 'Joe' AND title = 'boss' AND status = '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 //匹配,第三个参数为匹配模式 title LIKE '%match%'
113 $this->db->like('title', 'match', 'before/after/both');
114 $this->db->not_like();
115
116 //分组 GROUP BY title, date
117 $this->db->group_by('title', 'date');
118
119 //限制条数
120 $this->db->limit(0, 20);


  1. It‘s quite in here! Why not leave a response?



0