Table of Contents
Basic usage
Query constructor
Assistant function
Update data
Delete data
Conditional query method
Query expression
Home PHP Framework ThinkPHP What are the operating methods of Thinkphp5.0 on the database?

What are the operating methods of Thinkphp5.0 on the database?

May 31, 2023 pm 05:28 PM
thinkphp

Basic usage

Query operation

Db::query('select * from think_user where id=?',[8]);
Copy after login

Write operation

Db::execute('insert into think_user (id, name) values (?, ?)',[8,'thinkphp']);
Copy after login

Query constructor

Query data

find returns a record, and the returned result is a one-dimensional array. If the result does not exist, NULL is returned

Db::table('think_user')->where('id',1)->find();
Copy after login

select returns all records. The returned result is a two-dimensional array. If the result does not exist, an empty array is returned.

Db::table('think_user')->where('status',1)->select();
Copy after login

Assistant function

The system provides A db assistant function can make query more convenient:

db('user')->where('id',1)->find();
db('user')->where('status',1)->select();
Copy after login

Note: Using the db assistant function will reconnect to the database every time by default, while using the Db::name or Db::table method will do the same. Singleton. If the db function needs to use the same link, you can pass in the third parameter, for example

db('user',[],false)->where('id',1)->find();
db('user',[],false)->where('status',1)->select();
Copy after login

Add data

Use the insert method of the Db class to add data to the database Submit data

$data = ['foo' => 'bar', 'bar' => 'foo'];
Db::table('think_user')->insert($data);
Copy after login

Add multiple pieces of data

To add multiple pieces of data, just pass the data to be added directly to the insertAll method of the Db class

$data = [    ['foo' => 'bar', 'bar' => 'foo'],
    ['foo' => 'bar1', 'bar' => 'foo1'],
    ['foo' => 'bar2', 'bar' => 'foo2']
];
Db::name('user')->insertAll($data);
Copy after login

insertAll method adds data successfully and returns the number of successfully added items

Assistant function

// 添加单条数据
db('user')->insert($data);

// 添加多条数据
db('user')->insertAll($list);
Copy after login

Update data

update method returns The number of affected data, if no data is modified, returns 0

Db::table('think_user')->where('id', 1)->update(['name' => 'thinkphp']);
Copy after login

Update the value of a field: The setField method returns the number of affected data, if no data field is modified, returns 0

Db::table('think_user')->where('id',1)->setField('name', 'thinkphp');
Copy after login
自增或自减一个字段的值
// score 字段加 1
Db::table('think_user')->where('id', 1)->setInc('score');
// score 字段加 5
Db::table('think_user')->where('id', 1)->setInc('score', 5);
// score 字段减 1
Db::table('think_user')->where('id', 1)->setDec('score');
// score 字段减 5
Db::table('think_user')->where('id', 1)->setDec('score', 5);
Copy after login

Delay Update

Db::table('think_user')->where('id', 1)->setInc('score', 1, 10);
Copy after login

Delete data

// 根据主键删除
Db::table('think_user')->delete(1);
Db::table('think_user')->delete([1,2,3]);

// 条件删除    
Db::table('think_user')->where('id',1)->delete();
Db::table(&#39;think_user&#39;)->where(&#39;id&#39;,&#39;<&#39;,10)->delete();
Copy after login

Conditional query method

where

You can use the where method for AND conditional query:

Db::table(&#39;think_user&#39;)
    ->where(&#39;name&#39;,&#39;like&#39;,&#39;%thinkphp&#39;)
    ->where(&#39;status&#39;,1)
    ->find();
Copy after login

whereOr method

Db::table(&#39;think_user&#39;)
    ->where(&#39;name&#39;,&#39;like&#39;,&#39;%thinkphp&#39;)
    ->whereOr(&#39;title&#39;,&#39;like&#39;,&#39;%thinkphp&#39;)
    ->find();
Copy after login

Mixed query

where method and whereOr Methods often need to be mixed together in complex query conditions. Here is an example:

$result = Db::table(&#39;think_user&#39;)->where(function ($query) {
    $query->where(&#39;id&#39;, 1)->whereor(&#39;id&#39;, 2);
})->whereOr(function ($query) {
    $query->where(&#39;name&#39;, &#39;like&#39;, &#39;think&#39;)->whereOr(&#39;name&#39;, &#39;like&#39;, &#39;thinkphp&#39;);
})->select();
Copy after login

Query expression

Query expression supports most SQL queries Syntax is also the essence of ThinkPHP query language. The format of query expressions is:

where(&#39;字段名&#39;,&#39;表达式&#39;,&#39;查询条件&#39;);
whereOr(&#39;字段名&#39;,&#39;表达式&#39;,&#39;查询条件&#39;);
Copy after login
表达式 含义
EQ、= 等于(=)
NEQ、<> 不等于(<>)
GT、> 大于(>)
EGT、>= 大于等于(>=)
LT、< 小于(<)
ELT、<= 小于等于(<=)
LIKE 模糊查询
[NOT] BETWEEN (不在)区间查询
[NOT] IN (不在)IN 查询
[NOT] NULL 查询字段是否(不)是NULL
[NOT] EXISTS EXISTS查询
EXP 表达式查询,支持SQL语法
> time 时间比较
< time 时间比较
between time 时间比较
notbetween time 时间比较
Copy after login

The above is the detailed content of What are the operating methods of Thinkphp5.0 on the database?. For more information, please follow other related articles on the PHP Chinese website!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles