use/think/Db;
/******** 查詢構造器 *********/
// 插入記錄
// Db::table('sb_ad')->insert(['ad_name' => 6, 'ad_content' => 'thinkphp', 'status' => 1]);
// 更新記錄
// Db::table('sb_ad')
// ->where('ad_id', 2)
// ->update(['ad_name' => "hello"]);
// 查詢數據
// $list = Db::table('sb_ad')
// ->where('ad_id', 2)
// ->select();
// dump($list);
// 刪除數據
// Db::table('sb_ad')
// ->where('ad_id', 2)
// ->delete();
// 插入記錄
// Db::name('ad')->insert(['ad_name' => 2, 'ad_content' => '77777777777777']);
/****鏈式操作****/
// 查詢十個滿足條件的數據 并按照role_id倒序排列
// $list = Db::name('auth_access')
// ->field('role_id,rule_name')
// ->order('role_id', 'desc')
// ->limit(10)
// ->select();
// print_r($list);
//事務支持 在Mysql數據庫中請設置表類型為InnoDB
//把需要執行的事務操作封裝到閉包里面即可自動完成事務
// Db::transaction(function () {
// Db::table('sb_ad')->where('ad_id', 1)->select();
// Db::table('sb_add')->insert(['ad_name' => 10, 'ad_content' => 'thinkphp', 'status' => 1]);
// });
// 手動控制事務的提交
// 啟動事務
Db::startTrans();
try {
Db::table('sb_ad')
->insert(['ad_namea' => 13, 'ad_content' => 'thinkphp', 'status' => 1]);
Db::table('sb_ad')
->insert(['ad_name' => 11, 'ad_content' => 'thinkphp', 'status' => 1]);
// 提交事務
echo 'try';
Db::commit();
} catch (\Exception $e) {
// 回滾事務
echo 'catch';
Db::rollback();
}