Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use where method in thinkphp

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to use where method in thinkphp", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use where method in thinkphp" article.

In thinkphp, where method is used to filter the results of database operations, which is one of the coherent operation methods of model class. It can complete query operations including general query, expression query, shortcut query, interval query and combination query. The syntax is "instantiate object-> where ('condition')".

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

How to use 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, let's talk about the most commonly used but also the most complex where method for query. Where method is also one of the coherent operation methods of model class, which is mainly used for query and operation condition setting.

The usage of where method is not only the essence of ThinkPHP query language, but also an important part and highlight of ThinkPHP ORM. It can complete query operations including general query, expression query, shortcut query, interval query and combination query. The parameters of the where method support strings and arrays, and although you can also use objects, it is not recommended.

String condition

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

$User = M ("User"); / / instantiate the User object $User- > where ('type=1 AND status=1')-> select ()

The final generated SQL statement is

SELECT * FROM think_user WHERE type=1 AND status=1

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

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

Or use:

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

If the $id variable comes from a user submission or URL address, and if a non-numeric type is passed in, the query operation will be forced to be formatted into a numeric format.

The format type of string preprocessing supports specifying numbers, strings, etc. For more information, please see the parameter description of vsprintf method.

Array condition

The where use of array conditions is recommended by ThinkPHP.

General query

The simplest array query is as follows:

$User = M ("User"); / / instantiate the User object $map ['name'] =' thinkphp';$map ['status'] = 1 / pass the query condition into the query method $User- > where ($map)-> select ()

The final generated SQL statement is

SELECT * FROM think_user WHERE `name` = 'thinkphp' AND status=1 above is the content of this article on "how to use where method in thinkphp". I believe everyone has some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report