Codeigniter数据库操作

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

 

posted @ 2016-07-08 11:00  哦先生  阅读(578)  评论(0编辑  收藏  举报