注:执行SQL语句时,首先要确保已经与数据库链接:$this->db->database();
1、Select查询
(1)获取数据表所有记录(取回全部数据,相当于 select * ):
1)$query = $this->db->get('sites'); //sites为表名
2)$this->db->from('sites');(2)获取数据表中特定的字段: select$query = $this->db->get();
(3)排序: order_by$this->db->select('url','name','clientid'); //'url','name','clientid'为数据表字段$query = $this->db->get('sites');
$this->db->order_by();
帮助你设置一个 ORDER BY 子句。第一个参数是你想要排序的字段名。第二个参数设置结果的顺序,可用的选项包括asc(升序)或desc(降序), 或random(随机)。
$this->db->order_by("title", "desc");
// 生成: ORDER BY title DESC你也可以在第一个参数中传递你自己的字符串:
$this->db->order_by('title desc, name asc');
// 生成: ORDER BY title DESC, name ASC或者,多次调用本函数就可以排序多个字段。
$this->db->order_by("title", "desc");
$this->db->order_by("name", "asc");
// 生成: ORDER BY title DESC, name ASC
例:
(4)限制返回数据记录条数: limit$this->db->select('url','name','clientid');$this->db->order_by("name", "desc");$query = $this->db->get('sites');
(5)条件: where$this->db->select('url','name','clientid');//'url','name','clientid'为列名$this->db->orderby("name", "desc");$this->db->limit(5); // 最初五条数据$query = $this->db->get('sites');
注:$this->db->limit($pageSize,$start)$pageSize:每页显示条数$start:起始条数
1)‘=’的情况:
$this->db->where('clientid', '1'); //clientid属性 "1"为属性值
2)‘!=’的情况:
$this->db->where('url !=', 'www.mysite.com');$this->db->where('id >', '3');
3)多个where条件的情况:
$this->db->where('url !=','www.mysite.com');$this->db->where('id >', '3');
4)WHERE…OR的情况:或
$this->db->where('url !=','www.mysite.com' );5)like:$this->db->orwhere('url !=','www.anothersite.com' );
$this->db->like('title', 'match', 'before');// 生成: WHERE title LIKE '%match'$this->db->like('title', 'match', 'after');// 生成: WHERE title LIKE 'match%'$this->db->like('title', 'match', 'both');// 生成: WHERE title LIKE '%match%'
(6)组合查询: join
$this->db->from('sites');$this->db->join('people', 'sites.peopleid = people.id');
如:写个完整的查询
$this->db->select('url','name','clientid','people.surname AS client');$this->db->where('clientid', '3');$this->db->limit(5);$this->db->from('sites');$this->db->join('people', 'sites.clientid = people.id','LEFT');$this->db->order_by("name", "desc");$this->db->group_by(' clientid.id');(7) 显示查询结果:$query = $this->db->get();
2、Insert 添加在查询语句后加上下面这句话:$query = $this->db->get();
如果有多个结果,他们被保存在$row对象中,可以用一个 foreach 循环:foreach ( $query->result() as $row){print $row->url;print $row->name;print $row->client;}
如果我们只想要一个结果,它可以作为一个对象被返回, 或在这里当做一个$row数组if ( $query->num_rows() > 0) // $query->num_rows() 返回查询结果条数{$row = $query->row_array() ; // 返回结果为 数组print $row['url'];print $row['name'];print $row['client'];}
方法一:先建个数组,把要insert的值放在数组里.如下:其中url/name/clientid/type均为数据表属性值
$data = array('url' => 'www.mynewclient.com','name' => 'BigCo Inc','clientid' => '33','type' => 'dynamic');
$this->db->insert('sites', $data);
方法二:使用$this->db->set() 设置每一个值
3、Update 更新$this->db->set('url', 'www.mynewclinet.com');$this->db->set('name', 'BigCo Inc');$this->db->set('clientid', '33');$this->db->set('type', 'dynamic');
$this->db->insert('sites');
注:先定位要更新的记录,再Update
方式一:使用数组形式
$this->db->where('id', '1');$data = array('url' => 'www.mynewclient.com','name' => 'BigCo Inc','clientid' => '33','type' => 'dynamic'方式二: 使用$this->db->set() 设置每一个值);
$this->db->update('sites', $data);
4、Delete 删除$this->db->where('id', '1');
$this->db->set('url', 'www.mynewclinet.com');
$this->db->set('name', 'BigCo Inc');
$this->db->set('clientid', '33');
$this->db->set('type', 'dynamic');
$this->db-> update ('sites');
注:先定位要删除的记录,再
Delete
5、检测数据库是否执行成功$this->db->where('id', '2');
$this->db->delete('sites');
(1)
$this->db->affected_rows();
(2)
$new_id_number = $this->db->insert_id(); // 执行insert添加时,可以通过此函数返回添加之后记录的ID