600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > think php getfield thinkPHP数据查询常用方法总结【select find getField query】

think php getfield thinkPHP数据查询常用方法总结【select find getField query】

时间:2018-12-24 04:03:02

相关推荐

think php getfield thinkPHP数据查询常用方法总结【select find getField query】

thinkphp已经封装好了常用的查询方法,且都比较实用,对于不常用的查询框架也保留了原始查询方法query。$Model=newModel()//实例化一个model对象没有对应任何数据表

$Model->query("select*fromthink_userwherestatus=1");

如果刚学Thinkphp对框架不太了解可以用query($sql)和 execute($sql)两个方法可以实现任何的sql操作。query用于查询操作,execute用于非查询操作。但是框架已经封装好了常用的方法,且用起来更方便。

下面是最常用的查询方法:

1. select()//将所有数据查出,失败返回false,无结果返回null

$user=M('demo');

$data=$user->select();

dump($data);

//加入条件

$user->field('name,sex')->where('id>2')->order('age')->limit(3)->select();

//查询主键值为30的信息

$user->select('30');

//查询主键为21,23,27的值

$user->select('21,23,27');

2. find()//查询出一条数据

$user=M('demo');

//失败返回false

if($data=$user->find()){

dump($data);

}

//加入where条件

$user=M('demo');

$data=$user->field('name,sex')->where('id>2')->find();

dump($data);

//返回一维数组

$data->find('30');

$manager->where("username='$username'andpassword='$password'")->find();

3. getField()//获取列数据中的第一条

$user=M('demo');

$data=$user->getField('name');//默认第一个

//第二个参数位true则获取整列数据

$user->where("id=3")->getField('name',true);

//限制显示条数

$nickname=$User->where('status=1')->getField('nickname',8);

$nickname=$User->where('status=1')->limit(8)->getField('nickname',true);

//返回二维数组,键名为第一个

$nickname=$User->where('status=1')->getField('id,nickname,sex');

//使用连接符':'键名是id值,键值则是account:nickname连接组成的字符串

$result=$User->where('status=1')->getField('id,account,nickname',':');

还有详细的查询方法详见 ThinkPHP3.2手册中的 "模型>查询语句" 章节。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。