CI模型
CI框架模型类
1、新增
$this->db->insert('tableName',$data);
return $this->db->insert_id();
技巧:$data = array(
'name'=>$this->input->get('name'),
'sn'=>$this->input->get('sn'),
);
2、批量插入
$this->db->insert_batch('tableName', $data);
3、更新操作
$this->db->set('state', $state);
$this->db->where('id', $suits_id);
$this->db->update('tableName');
return $this->db->affected_rows();
4、查询操作
①$query->result_array(); //返回多条数组
②$query->result(); //返回对象
③$query->row_array(); //返回对象
④$query->row(); //返回单条数组对象
⑤return $query->num_rows() == 0 ? null : $query->row_array();//判断是否存在符合条件的查询
5、删除操作
$this->db->where('id',$id);
$this->db->delete('tableName');
return $this->db->affected_rows();
6、获取查询的总数
$this->db->count_all_results();
7、分页
$this->db->limit($per_page, $offset);//$per_page 每页显示条数 $offset 偏移量
8、查询多条
$this->db->from('tableName');
$query = $this->db->get();
//计数
$this->db->from('tableName');
$total_rows = $this->db->count_all_results();
return array('query' => $query, 'total_rows' => $total_rows);
技巧:返回query可以自定义返回结果类型
获取总数利于分页
9、命名规范
①curd 对应 add getRow getRows del set
②条件 getRowById delById
9、打印sql语句
$this->db->last_query();
10、清空数据
$this->db->truncate('tableName');
11、自定义sql语句
$this->db->query($sql);
return $query->result_array();
//查询绑定
$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));

浙公网安备 33010602011771号