About database operations of thinkphp5

jacklove
Release: 2023-04-01 09:48:01
Original
1742 people have browsed it

1.Database configuration



##2.

query executeOriginal ecologysqlStatement addition, deletion, modification and query
$result = Db::execute('insert into log(user_id, ip) values(1, 11231)'); dump($result); $result = Db::query('select * from log'); echo '
'; var_dump($result);
Copy after login


3.

Parameter bindingNamed placeholder binding
$str = 'insert into log(user_id, ip) values(?, ?)'; $result = Db::execute($str, [1, '12312']); $result = Db::query('select * from log where id = ?', [4]); //占位符 Db::execute('insert into log(user_id, ip) values(:user_id, :ip)', ['user_id'=>12, 'ip'=>'5555']);
Copy after login

4.

Query Constructor


//添加: Db::table('log')->insert(['user_id'=>1, 'ip'=>'654321']); //更新 Db::table('log') ->where('id', 12) ->update(['user_id'=>123]); //查询数据 $list = Db::table('log') ->where('id', 12) ->select(); //删除数据 Db::table('log') ->where('id', 10) ->delete();
Copy after login

How to query the table without adding a prefix:

Db::name('log')->insert(['user_id'=>44, 'ip'=>5555]);
Copy after login

##5.

DB

Chain operation

Methods to support chain query:

Method name Description ##select find insert update dalete Aggregation query Things Support

Query Database

Query a single record

Insert record

##Update Record

Delete record

##value
Query value

column
Query column

chunk
Chunk query

##count

##6.

//自动控制事物 Db::transaction(function (){ Db::table('log')->delete(2); Db::table('log')->insert(['user_id'=>123]); }); //手动控制事物的提交 //启动事物 Db::startTrans(); try { Db::table('log') ->where(2); Db::table('log') ->insert(['user_id' => 213]); Db::commit(); } catch (Exception $e){ Db::rollback(); }
Copy after login
This article explains the database operations of thinkphp5. For more related content, please pay attention to the php Chinese website. Related recommendations:
thinkphp Detailed explanation of distributed database

##How to link the database through ThinkPHP


How to connect multiple databases through thinkphp


The above is the detailed content of About database operations of thinkphp5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!