search
HomeBackend DevelopmentPHP TutorialApplication explanation of where method

The usage of

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

String condition

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

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

Array condition

Normal query

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

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

Expression query

$map['字段1']  = array('表达式','查询条件1');$map['字段2']  = array('表达式','查询条件2');$Model->where($map)->select(); // 也支持
$map['id']  = array('eq',100);

represents the query condition represented by id = 100

$map['id']  = array('neq',100);

The query condition is id 100

$map['id']  = array('gt',100);

represents the query condition is id > 100

$map['id']  = array('egt',100);

represents the query condition is id >= 100

$map['id']  = array('lt',100);

represents The query condition is id

$map['id']  = array('elt',100);

The query condition represented is id

[NOT] LIKE: Same as sql's LIKE

$map['name'] = array('like','thinkphp%');

The query condition becomes name like 'thinkphp%'

$map['a'] =array('like',array('%thinkphp%','%tp'),'OR');$map['b'] =array('notlike',array('%thinkphp%','%tp'),'AND');

The generated query condition is: (a like '%thinkphp%' OR a like '%tp') AND (b not like '% thinkphp%' AND b not like '%tp')

[NOT] BETWEEN: Same as sql's [not] between, query conditions support strings or arrays, for example:

$map['id']  = array('between','1,8');
$map['id']  = array('between',array('1','8'));

[NOT] IN: 同sql的[not] in ,查询条件支持字符串或者数组,例如:

$map['id']  = array('not in','1,5,8');
$map['id']  = array('not in',array('1','5','8'));

EXP:表达式,支持更复杂的查询情况

$map['id']  = array('exp',' IN (1,3,8) ');

等同于

$map['id']  = array('in','1,3,8');

组合查询

$User = M("User"); // 实例化User对象$map['id'] = array('neq',1);$map['name'] = 'ok';$map['_string'] = 'status=1 AND score>10';$User->where($map)->select();

最后得到的查询条件就成了:( `id` != 1 ) AND ( `name` = 'ok' ) AND ( status=1 AND score>10 )

复合查询

$where['name']  = array('like', '%thinkphp%');$where['title']  = array('like','%thinkphp%');$where['_logic'] = 'or';$map['_complex'] = $where;$map['id']  = array('gt',1);

等同于

$where['id'] = array('gt',1);$where['_string'] = ' (name like "%thinkphp%")  OR ( title like "%thinkphp") ';

查询条件是 
( id > 1) AND ( ( name like '%thinkphp%') OR ( title like '%thinkphp%') )

本文讲解了where方法的应用更多相关内容请关注php中文网。

相关推荐:

ThinkPHP 双重循环遍历输出 的相关内容

ThinkPHP5快速入门 方法的介绍

介绍ThinkPHP使用步骤

The above is the detailed content of Application explanation of where method. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.