How does thinkphp query the database?
Database Query
ThinkPHP has built-in very flexible query methods that can quickly perform data query operations.
Query conditions can be used for any operation such as CURD, and can be passed in as parameters of the where method.
ThinkPHP can support the direct use of strings as query conditions, but in most cases it is recommended to use index arrays or objects as query conditions, because it is safer.
Query method
1. Use strings as query conditions
This is the most traditional method, but it is safer Not high, for example:
$User = M("User"); // 实例化User对象 $User->where('type=1 AND status=1')->select();
The last generated SQL statement is
SELECT * FROM think_user WHERE type=1 AND status=1
2. Use arrays as query conditions
$User = M("User"); // 实例化User对象 $condition['name'] = 'thinkphp'; $condition['status'] = 1; // 把查询条件传入查询方法 $User->where($condition)->select();
The last generated SQL statement Yes
SELECT * FROM think_user WHERE 'name'='thinkphp' AND status=1
If you perform a multi-field query, the default logical relationship between fields is logical AND, but the default logical judgment can be changed using the following rules, by using _logic to define the query logic:
$User = M("User"); // 实例化User对象 $condition['name'] = 'thinkphp'; $condition['account'] = 'thinkphp'; $condition['_logic'] = 'OR'; //定义查询逻辑 // 把查询条件传入查询方法 $User->where($condition)->select();
The final generated SQL statement is
SELECT * FROM think_user WHERE 'name'='thinkphp' OR `account`='thinkphp'
3. Use the object method to query (here, take the stdClass built-in object as an example)
$User = M("User"); // 实例化User对象 // 定义查询条件 $condition = new stdClass(); $condition->name = 'thinkphp'; $condition->status= 1; $User->where($condition)->select();
The final generated SQL statement is the same as above
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
The effect of using object mode query and using array query is the same, and they are interchangeable. In most cases, we recommend using array mode to be more efficient. , later we will use the array method as an example to explain the specific query language usage.
Expression query
The above query condition is just a simple equality judgment. Query expressions can be used to support more SQL query syntax and can be used Query in array or object mode (only array mode is used as an example below), the query expression format is:
$map['字段名'] = array('表达式','查询条件');
Expressions are not case-sensitive. The supported query expressions are as follows, respectively. The meaning is:
##
$map['id'] = array('eq',100); id = 100; $map['id'] = array('egt',100);id >= 100 $map['name'] = array('like','thinkphp%'); name like 'thinkphp%' 模糊查询 $map['a'] =array('like',array('%thinkphp%','%tp'),'OR');$map['b'] =array('notlike',array('%thinkphp%','%tp'),'AND'); (a like '%thinkphp%' OR a like '%tp') AND (b not like '%thinkphp%' AND b not like '%tp')This article comes from the ThinkPHP framework technical article column:
http://www.php.cn/phpkj/thinkphp /
The above is the detailed content of How thinkphp queries the database. For more information, please follow other related articles on the PHP Chinese website!

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor