Home  >  Article  >  Backend Development  >  About database operations of thinkphp5

About database operations of thinkphp5

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

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


3.

Parameter binding Named 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']);

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

How to query the table without adding a prefix:

Db::name('log')->insert(['user_id'=>44, 'ip'=>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('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();
}
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