Home  >  Article  >  PHP Framework  >  ThinkPHP query data select(findAll) method

ThinkPHP query data select(findAll) method

步履不停
步履不停Original
2019-06-25 16:31:156539browse

ThinkPHP query data select(findAll) method

ThinkPHP query data

ThinkPHP query data mainly provides the following types of queries:

  1. select: ordinary query, the same as findAll() method
  2. find: Get a record that meets the query conditions
  3. getBy dynamic query: Get a record that meets the query conditions based on a certain field
  4. getField: Get the value of a certain field Or an index array of multiple fields
  5. Interval query: Get the interval records that meet the query conditions
  6. Statistical query: Get the statistical data that meets the query conditions
  7. Positioning query: Get the match One or more records of query conditions
  8. Native SQL query: Supports querying or performing operations in native SQL

select()

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

Read operation

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.

Data display template

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[&#39;uid&#39;]}</td>
        <td>{$vo[&#39;username&#39;]}</td>
        <td>{$vo[&#39;email&#39;]}</td>
        <td>{$vo[&#39;regdate&#39;]|date=&#39;Y-m-d H:i&#39;,###}</td>
    </tr>
    </volist>
</table>

field() Query the specified field

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(&#39;username,email&#39;)->select();

Use query Condition

Using ThinkPHP coherent operation, you can easily use query conditions for data query. Below are some examples of simple query conditions.

where() condition

……   
    // 构造查询条件
    $condition[&#39;username&#39;] = &#39;Admin&#39;;
    // 查询数据
    $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".

ORDER BY sorting

Use ORDER BY in the query to sort the data:

……   
    // 查询数据
    $list = $Dao->order(&#39;uid DESC&#39;)->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.

LIMIT limit

Use LIMIT in the query to limit the number of records returned by the data:

……   
    // 查询数据
    $list = $Dao->limit(&#39;4,5&#39;)->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.

Continuous operation

ThinkPHP allows each method in the data object to be written together for operation, such as:

$list = $Dao->order(&#39;uid DESC&#39;)->limit(&#39;4,5&#39;)->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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:thinkphp exp usageNext article:thinkphp exp usage