Home >PHP Framework >ThinkPHP >ThinkPHP query data select(findAll) method
ThinkPHP query data mainly provides the following types of queries:
select() is the most commonly used ordinary query method in ThinkPHP, and the result is a two-dimensional array. findAll() is an alias for the select() method, and it is recommended to use select().
The following example reads and displays all the data in the user table:
public function read(){ $Dao = M("User"); // 查询数据 $list = $Dao->select(); //dump($list);// 用 dump() 可以在调试阶段查看数据是否已读取 // 模板变量赋值 $this->assign("list", $list); // 输出模板 $this->display(); }
Assume that the class file corresponding to the above example is Lib/Action/ IndexAction.class.php, then the corresponding template file is Tpl/default/Index/read.html.
The template file is used to display the data of the User table just read. During the learning phase, if you do not want to use templates, you can also directly use the foreach syntax to display the read data directly within the read() operation. The following is the corresponding code snippet of the template. We display the read data in a table:
<table border="1"> <tr> <th width="10%">ID</th> <th width="30%">用户名</th> <th width="30%">电子邮件</th> <th>注册时间</th> </tr> <volist name="list" id="vo"> <tr> <td align="center">{$vo['uid']}</td> <td>{$vo['username']}</td> <td>{$vo['email']}</td> <td>{$vo['regdate']|date='Y-m-d H:i',###}</td> </tr> </volist> </table>
select() method defaults to query all fields Data, if you want to query one or some fields, you need to use the filed() method.
filed() is a method that belongs to the continuous operation of ThinkPHP. For example, in the above example, only the user name and email address are queried, then the query method should be changed to:
$list = $Dao->field('username,email')->select();
Using ThinkPHP coherent operation, you can easily use query conditions for data query. Below are some examples of simple query conditions.
…… // 构造查询条件 $condition['username'] = 'Admin'; // 查询数据 $list = $Dao->where($condition)->select(); ……
The above query is the data of username='Admin'. For more detailed information about ThinkPHP where conditions, please refer to "ThinkPHP Where conditions".
Use ORDER BY in the query to sort the data:
…… // 查询数据 $list = $Dao->order('uid DESC')->select(); ……
This example is that the data is queried according to ORDER BY uid DESC, and the order() method The meaning of the parameters in is exactly the same as that in the SQL statement.
Use LIMIT in the query to limit the number of records returned by the data:
…… // 查询数据 $list = $Dao->limit('4,5')->select(); ……
This example is to take out the 5th-10th records, in the limit() method The meaning of the parameter is exactly the same as LIMIT in the SQL statement.
ThinkPHP allows each method in the data object to be written together for operation, such as:
$list = $Dao->order('uid DESC')->limit('4,5')->select();
For more ThinkPHP related technical articles, please visit Learn in the ThinkPHP Tutorial column!
The above is the detailed content of ThinkPHP query data select(findAll) method. For more information, please follow other related articles on the PHP Chinese website!