对数据的读取
- $m=M(‘User’);
- $m->select(); //获取所有数据,以数组形式返回
- $m->find(); //只返回一条数据,默认id为1,也以数组形式返回,如* 果要返回id为2,则用$m->find(2);
- $m->delete()会删除所有数据,要删除具体数据,可以$m->where(‘id=2’)->delete()或$m->delete(2),返回值是删除的行数
- $m->delete()会删除所有数据,要删除具体数据,可以$m->where(‘id=2’)->delete()或$m->delete(2),返回值是删除的行数
- 查询返回的数组元素的类型都是字符串string类型
- 更新后页面跳转,success(“桌面要显示的内容”,”要跳转的页面,如’index’”);
- 数据更新失败跳转,$this->error(“桌面要显示的内容”,”要跳转的页面,如’index’”);
- 添加数据返回的add()方法,返回值是id号
id | username | sex |
---|---|---|
2 | tong | 1 |
3 | nice | 0 |
4 | enen | 0 |
$m=M('User');
$m->where('id=2')->getField('username')
//select 'username' from user where(id=4) limit 1
$m->where('sex=0 and username=tong')->find();
//错误查询 结果是select * from user where(id=2 and username=tong) limit 1
$m->where("sex=0 and username='tong'")->find();
//查询正确,结果是select * from user where(id=2 and username='tong') limit 1
//数组查询方式(推荐)
$data['sex']=1;
$data['username']='tong';
$m->where($data)->find();
//select * from user where sex=1 and username='tong' limit 1 默认是 and
$data['sex']=1;
$data['username']='tong';
$data['_logic']='or';
$m->where($data)->find();
//select * from user where sex=1 OR username='tong' limit 1
$data['id']=array('GT',2);
$m->where($data)->find();
//select * from user where id>3 limit 1
$m->where($data)->select();
//select * from user where id>3 ;返回是二维数组
//同理,GT(great than)可以是
//lt(less than)
//EQ(equal)
//EGT(大于等于)
//ELT(小雨等于)
//NEQ(不等于)
$data['username']=array('like','%n%');
$m->where($data)->select();
//select * from user where username LIKE '%n%'
$data['username']=array('notlike','%n%');
$m->where($data)->select();
//select * from user where username NOT LIKE '%n%' ; notlike中间没有空格
$data['username']=array('like',array('%n%','%o%'));
$m->where($data)->select();
//select * from user where(username LIKE '%n%' OR username LIKE '%t%')
$data['username']=array('like',array('%n%','%o%'),'and');
$m->where($data)->select();
//select * from user where(username LIKE '%n%' AND username LIKE '%t%')
//模糊查询可以接多个通配符,如 where('%' and '%' and '%')
$data['id']=array('between',array(3,4));
$m->where($data)->select();
//select * from user where id BETWEEN 3 AND 4
$data['id']=array('not between',array(3,4));
$m->where($data)->select();
//select * from user where id NOT BETWEEN 3 AND 4; not和between必须隔开
$data['id']=array('in',array(3,4));
$m->where($data)->select();
//select * from user where id in(3,4)
$data['id']=array('not in',array(3,4));
$m->where($data)->select();
//select * from user where id not in(3,4);注意:not和in必须隔开
//区间查询:
$data['id']=array(array('gt',4),array('lt',10));
$m->where($data)->select();
//select * from user where( id>4 AND id<10 ); 默认是and
$data['id']=array(array('gt',4),array('lt',10),'or');
$m->where($data)->select();
//select * from user where( id>2 OR id<10)
$data['username']=array(array('like','%t%'),array('like','%g%'),'tong','or');
$m->whrere($data)->select();
//select * from user where(username LIKE '%t%' OR username LIKE '%g%' OR username='tong')
$m->count();
//返回表中一共有多少条数据
$m->where("sex=1")->count();
//select COUNT(*) AS tp_count from user where(sex=1) limit 1
$m->max('id');
//select MAX(id) AS tp_max from user limit 1
$m->ave("id");
//select AVG(id) AS tp_avg from user limit 1
//可以用的
//sum
//avg
//count
//max
//min
//SQL直接查询
//query 主要用于读取
//execute 主要用于数据更新
//实例化一个空的Model
$m->M();
$m->query("select * from user where id>2");
//select * from user where id>2;无法执行返回bool类型false;
$m->execute("insert into user(username)values('zt')");
//insert into user(username)values('zt'),返回影响行数,失败返回false