Home >Backend Development >PHP Tutorial >About database operations of thinkphp5

About database operations of thinkphp5

jacklove
jackloveOriginal
2018-06-15 11:46:041851browse

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);


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;]);

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();

How to query the table without adding a prefix:

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

##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();
}
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!

Statement:
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