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.
The operating environment of this article: Windows 10 system, ThinkPHP version 3.2, Dell G3 computer.
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 '账号/密码错误'; } }
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() 主键查询
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);
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");
To obtain the value of a in the result, use:
$res["a"]
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!