Home > PHP Framework > ThinkPHP > Let's talk about Thinkphp 5.0's operation of database (key code)

Let's talk about Thinkphp 5.0's operation of database (key code)

青灯夜游
Release: 2022-09-23 19:28:36
forward
1521 people have browsed it

A standard website must be inseparable from database operations. In this set of courses, I will work with you to uncover the mystery of ThinkPHP5 data operations, and happily use ThinkPHP5 to operate the database with you, making database operations more changeable. Pleasure.

Let's talk about Thinkphp 5.0's operation of database (key code)

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 result returned 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, and the result returned is a two-dimensional array. If the result does not exist, NULL is returned. Exists, returns an empty array

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

Assistant function

The system provides a db assistant function for more convenient query:

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 is a 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 Db The insert method of the class submits data to the database

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

Add multiple pieces of data

Add multiple pieces of data and pass it directly to the insertAll method of the Db class The data that needs to be added can be

$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, no data is modified and returns 0

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

Update the value of a certain field: setField method returns the number of affected data, no data is modified The data field 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

Delayed 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('think_user')->where('id','<&#39;,10)->delete();
Copy after login

Conditional query method

where

You can use the where method to query AND conditions:

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

whereOr method

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

Mixed query

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

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

Query expression

Query expression supports most SQL query syntax and is also the essence of ThinkPHP query language. The usage format of query expression is:

where('字段名','表达式','查询条件');
whereOr('字段名','表达式','查询条件');
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

[Related tutorial recommendations: thinkphp framework]

The above is the detailed content of Let's talk about Thinkphp 5.0's operation of database (key code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.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