Home > PHP Framework > ThinkPHP > body text

How to use where method in thinkphp

WBOY
Release: 2022-06-08 16:48:24
Original
3516 people have browsed it

In thinkphp, the where method is used to filter the results of database operations. It is one of the coherent operation methods of the model class. It can complete ordinary queries, expression queries, quick queries, interval queries, and combined queries. For query operations, the syntax is "instantiate object->where('condition');".

How to use where method in thinkphp

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

How to use the where method in thinkphp

The where method can be used to filter the results of database operations. That is, the where clause in the SQL query statement.

Today I will tell you about the most commonly used but also the most complex where method for querying. The where method is also one of the coherent operation methods of the model class and is mainly used for querying and setting operating conditions.

The usage of the where method is the essence of the ThinkPHP query language and an important part and highlight of the ThinkPHP ORM. It can complete queries including ordinary queries, expression queries, quick queries, interval queries, and combined queries. operate. The parameters of the where method support strings and arrays. Although objects can also be used, it is not recommended.

String conditions

Use string conditions to directly query and operate, for example:

$User = M("User"); // 实例化User对象
$User->where('type=1 AND status=1')->select();
Copy after login

The final generated SQL statement is

SELECT * FROM think_user WHERE type=1 AND status=1
Copy after login

If you use version 3.1 or above, when using string conditions, it is recommended to cooperate with the preprocessing mechanism to ensure more security, for example:

$Model->where("id=%d and username='%s' and
xx='%f'",array($id,$username,$xx))->select();
Copy after login

or use:

$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
Copy after login

If the $id variable comes from If the user submits or URL address, if the input is a non-numeric type, it will be forcibly formatted into a numeric format before querying.

The string preprocessing format type supports specifying numbers, strings, etc. For details, please refer to the parameter description of the vsprintf method.

Array condition

The where usage of array condition is the usage recommended by ThinkPHP.

Normal query

The simplest array query method is as follows:

$User = M("User"); // 实例化User对象
$map['name'] = 'thinkphp';
$map['status'] = 1;
// 把查询条件传入查询方法
$User->where($map)->select();
Copy after login

The final generated SQL statement is

SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
Copy after login

Recommended learning: "PHP Video tutorial

The above is the detailed content of How to use where method in thinkphp. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!