CodeIgniter数据库的使用

140 阅读1分钟
载入database,

1
2
3
$this->load->database();
也可在config文件夹下的autoload.php文件中修改:
$autoload['libraries'] =array('database');

这样,就可以使用了。
查询单条数据:

1
2
3
4
5
$this->db->select('title, content, date');
$query=$this->db->get_where('test',array('id'=>$id));
//$this->db->where('id', $id);
//$query = $this->db->get('test');
return$query->row_array();

查询多条数据:

1
2
3
4
5
6
$this->db->select('title, content, date');
$query=$this->db->get('test');
//$this->db->from('test');
//$query = $this->db->get();
$this->db->order_by("title","desc");
return$query->result_array();

获取数据库中的记录总数:

1
2
3
4
$this->db->count_all(‘test');

$this->db->where('aid',$aid);
return$this->db->count_all_results('test');