Home >Backend Development >PHP Tutorial >About database operations of thinkphp5
##2.
$result = Db::execute('insert into log(user_id, ip) values(1, 11231)'); dump($result); $result = Db::query('select * from log'); echo '<pre class="brush:php;toolbar:false">'; var_dump($result);
$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.
//添加: 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
Description | |
Query Database | |
Query a single record | |
Insert record | |
##Update Record |
dalete |
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!