Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the find method of thinkphp

Shulou Source: shulou.com Published: 2022-06-01 12:31:14 09月14日 Update

This article mainly introduces the relevant knowledge of "how to use the find method of thinkphp". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to use the find method of thinkphp" can help you solve the problem.

In thinkphp, the find method is used to get a row of records that meet the conditions in the data table. This function can only return a row of records. If you get multiple records that meet the conditions, you will return the first record. The result is an array, and the key of the array corresponds to the field in sql.

This article operating environment: Windows10 system, ThinkPHP3.2 version, Dell G3 computer.

How to use the find method of thinkphp

The ThinkPHP find () method is similar to the usage of select (), except that find () always queries only one piece of data, that is, the system automatically adds a LIMIT 1 restriction.

When confirming that the data record queried 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"); / / construct query conditions $condition ['username'] =' Admin'; $condition ['password'] = MD5 (' 123456'); / / query data $list = $Dao- > where ($condition)-> find (); if ($list) {echo 'account is correct' } else {echo 'account / password error';}}

Another difference from select () is that find () returns an one-dimensional array, which can output the value of the array unit directly in the template without having to loop through tags such as volist:

{$list ['username']} find () primary key query

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

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

The primary key of the user table is uid, and this example will query the data of uid=1, which is one of the ActiveRecords schema implementations, concise 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, assuming:

$res=$model- > find (filed= "a dint bje c")

To get the value of an in the result:

This is the end of $res ["a"] on "how to use the find method of thinkphp". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Method query array data condition different one row knowledge result account parameter system industry output practical concise intuitive example content function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology Xiaomi macOS Shulou Information