find()
ThinkPHP find() method is similar to select(). The difference is that find() always queries only one piece of data, that is, the system automatically adds a LIMIT 1 limit.
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() is that 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() primary key query
When the condition parameter of the find() query is the table primary key, you can directly Parameters are written into the method, such as:
$Dao = M("User"); $list = $Dao->find(1);
user The primary key of the table is uid. This example will query the data with uid=1. This is one of the ActiveRecords mode implementations, simple and intuitive.
The above introduces the ThinkPHP find method to query a data record, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.