AR模型
Active Record
1、配置继承AR模型
databese.php
$active_record = TRUE;
2、$this->db->get('blog_user');方法
$res=$this->db->get('blog_user');
foreach($res->result() as $item){
echo $item->name;
echo "<hr/>";
}
3、$this->db->insert('blog_user',$data);
//返回bool
$data=array(
'name'=>'jack',
'password'=>md5('123456'),
'email'=>'[email protected]',
);
$bool=$this->db->insert('blog_user',$data);
if($bool){
echo $this->db->affected_rows();
echo "<br/>";
echo $this->db->insert_id();
}
4、$this->db->update('blog_user',array('id'=>3),$data);
//参数一:表名 参数二:关联数组(更改id是2的行) 参数三:内容
$data=array(
'email'=>'www.ptbird.com',
'password'=>md5('123456789'),
);
$bool=$this->db->update('blog_user',array('id'=>3),$data);
var_dump($bool);
5、$this->db->delete('blog_user',array('id'=>2));
array('id'=>2); 关联数组
AR 连贯操作(分页)
1、$res=$this->$db->select('id,name')
->from('blog_user')
->where('id >=',3) //id后面有一个空格!
->limit(3,2) //跳过两个取出前三个 就是 0 1 2 3 4 5 取出 2 3 4
->order_by('id desc') //排序
->get('blog_user');
foreach($res->result() as $item){
echo $item->id;
}
2、取最近的一条sql语句
echo $this->db->last_query();
3、where问题
$this->db->where('name =','mary')->get('blog_user');
//默认是等号 可以不写
$this->db->where('name !=','mary')->get('blog_user');
//不等于
$this->db->where (array('name'=>'mary','id >='=>2))->get('blog_user');
//多个选择条件