About database operations of thinkphp5

jacklove
Release: 2023-04-01 09:48:01
Original
1762 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 &#39;<pre class="brush:php;toolbar:false">&#39;;
var_dump($result);
Copy after login


3.

Parameter binding Named placeholder binding
$str = &#39;insert into log(user_id, ip) values(?, ?)&#39;;
$result = Db::execute($str, [1, &#39;12312&#39;]);

$result = Db::query(&#39;select * from log where id = ?&#39;, [4]);

//占位符
Db::execute(&#39;insert into log(user_id, ip) values(:user_id, :ip)&#39;, [&#39;user_id&#39;=>12, &#39;ip&#39;=>&#39;5555&#39;]);
Copy after login

4.

Query Constructor


//添加:
Db::table(&#39;log&#39;)->insert([&#39;user_id&#39;=>1, &#39;ip&#39;=>&#39;654321&#39;]);

//更新
Db::table(&#39;log&#39;)
    ->where(&#39;id&#39;, 12)
    ->update([&#39;user_id&#39;=>123]);

//查询数据
$list = Db::table(&#39;log&#39;)
    ->where(&#39;id&#39;, 12)
    ->select();

//删除数据
Db::table(&#39;log&#39;)
    ->where(&#39;id&#39;, 10)
    ->delete();
Copy after login

How to query the table without adding a prefix:

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

##5.

DB

Chain operation

Methods to support chain query:

Method nameDescription##selectfindinsertupdatedaleteAggregation queryThings 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(&#39;log&#39;)->delete(2);
    Db::table(&#39;log&#39;)->insert([&#39;user_id&#39;=>123]);
});

//手动控制事物的提交
//启动事物
Db::startTrans();
try {
    Db::table(&#39;log&#39;)
        ->where(2);
    Db::table(&#39;log&#39;)
        ->insert([&#39;user_id&#39; => 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
Popular Tutorials
More>
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!