CodeIgniter 之数据库类

来源:互联网 发布:微博数据分析报告 编辑:程序博客网 时间:2024/05/24 04:31

application/config/database.php    若需存储多个连接的值可把配置文件存放在多维数组里

$db['default']['hostname']      $db['test']['hostname']

$active_group = 'test/default';  参数可设置需要连接的组


1. 多结果标准查询
     对象形式:$query->result();
     数组形式:$query->result_array();
     返回当前请求的行数:$query->num_rows()

2.单结果标准查询
     对象形式:$query->row();
     数组形式:$query->row_array();    

3.插入时所影响的记录行数:$this->db->affected_rows();

4.获取某一行的数据:$query->row_array(2); // 数字表示要获取的行

5.释放当前查询所占用的内存: $query->free_result();

6.查询辅助函数

    $this->db->insert_id()  执行数据插入是的ID

    $this->db->affected_rows()    insert、update后被影响的行数

    $this->db->count_all(‘表名’)    返回指定表的总行数

    

Active Record 类


$this->db->get('表名');   获取一个表的全部数据

$this->db->get('表名',$limit,$offset); 获取一个表从$offset记录开始得到$limit条数据

$this->db->get_where('表名',$where)  获取一个带有查询条件的数据
                 ||

$this->db->get('表名');  $this->db->where(array('id'=>$id));

$this->db->distinct();   关键字distinct,过滤重复字段

$this->db->having();

$this->db->like();

$this->db->order_by('title','desc');

$this->db->limit(10,20);   //10表示返回数量,20表示偏移量

$this->db->set();   //插入或更新可设置参数对应的值

$this->db->insert();

$this->db->update();

$this->db->delete();


数据库工具类

加载工具类:$this->load->dbutil();

备份数据库或表(用于备份数据不大的表):

$this->load->dbutil();  //加载数据库工具类

$backup =& $this->dbutil->backup(); //备份整个数据库并赋值给一个变量

//加载文件辅助寒素并将文件写入服务器

$this->load->helper('file');

write_file('/path/to/mybackup.gz',$backup);

//加载文件辅助函数并将文件发送到你的桌面

$this->load->helper('download');

force_download('mybackup.gz',$backup);








0 0
原创粉丝点击