Home > PHP Framework > ThinkPHP > body text

How to use thinkphp's find method

WBOY
Release: 2022-02-24 16:08:50
Original
3988 people have browsed it

In thinkphp, the find method is used to obtain a row of records in the data table that meets the conditions. This function can only return one row of records. If multiple records that meet the conditions are obtained, the first record will be returned. The result It is an array, and the key of the array corresponds to the field in SQL.

How to use thinkphp's find method

The operating environment of this article: Windows 10 system, ThinkPHP version 3.2, Dell G3 computer.

How to use thinkphp's find method

ThinkPHP find() method is a method similar to select(). The difference is that find() always queries only one piece of data, which is the system LIMIT 1 limit is automatically added.

When it is confirmed that the queried data record can only be one record, it is recommended to use the find() method to query, such as user login account detection:

public function chekUser(){
    header("Content-Type:text/html; charset=utf-8");
    $Dao = M("User");
    
    // 构造查询条件
    $condition['username'] = 'Admin';
    $condition['password'] = MD5('123456');
    // 查询数据
    $list = $Dao->where($condition)->find();
    if($list){
        echo '账号正确';
    }else{
        echo '账号/密码错误';
    }
}
Copy after login

Another difference from select() Because find() returns a one-dimensional array, you can directly output the value of the array unit in the template without using labels such as volist to loop the output:

{$list['username']}
find() 主键查询
Copy after login

When the condition parameter of the find() query is the table primary key You can directly write the parameters into the method, such as:

$Dao = M("User");
$list = $Dao->find(1);
Copy after login

The primary key of the user table is uid. This example will query the data with uid=1. This is one of the ActiveRecords mode implementations, simple and intuitive.

The find method returns a row of records, and the result is an array. The key of the array corresponds to the field in SQL. Assume:

$res=$model->find(filed="a,b,c");
Copy after login

To obtain the value of a in the result, use:

$res["a"]
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use thinkphp's find method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template