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

前端之家收集整理的这篇文章主要介绍了thinkPHP数据查询常用方法总结【select,find,getField,query】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了thinkPHP数据查询常用方法分享给大家供大家参考,具体如下:

thinkPHP已经封装好了常用的查询方法,且都比较实用,对于不常用的查询框架也保留了原始查询方法query

query("select * from think_user where status=1");

如果刚学ThinkPHP对框架不太了解可以用query($sql)execute($sql) 两个方法可以实现任何的sql操作。query

用于查询操作

execute

用于非查询操作

。但是框架已经封装好了常用的方法,且用起来更方便。

下面是最常用的查询方法

1. select()

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()

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' and password = '$password'")->find();

3. getField()

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手册中的 "模型>查询语句" 章节。

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

原文链接:https://www.f2er.com/thinkphp/17766.html

猜你在找的ThinkPHP相关文章