網站首頁 Thinkphp 正文
注意事項:
1.先在database.php中配置好數據庫
2.只要是數據庫操作必須引用 use/think/Db;嚴格區分大小寫。
下面是方法:
public function hello5()
{
//所有查詢必須 use/think/Db;
/* 1 配置數據庫
* 2 使用DB 命名空間
*
*/
/****************tp5中使用原生語句*******************/
//query 用于查詢 其他的用execute
// 插入記錄
// $result = Db::execute('insert into sb_ad (ad_name, ad_content ,status) values (1, "456",1)');
// dump($result);
// 更新記錄
// $result = Db::execute('update sb_ad set ad_name = "framework" where ad_id = 1 ');
// dump($result);
// 查詢數據
// $result = Db::query('select * from sb_ad where ad_id = 1');
// print_r($result);
// 刪除數據
// $result = Db::execute('delete from sb_ad where ad_id = 2 ');
// dump($result);
//其它操作
// 顯示數據庫列表
// $result = Db::query('show tables from tpshop1');
// print_r($result);
// 清空數據表
// $result = Db::execute('TRUNCATE table sb_ad');
// dump($result);
/**************多個數據庫操作************/
//在application/config.php中加入配置
//例子:
/*
* // 數據庫配置1
'db2' => [
// 數據庫類型
'type' => 'mysql',
// 服務器地址
'hostname' => '127.0.0.1',
// 數據庫名
'database' => 'tpshop2',
// 數據庫用戶名
'username' => 'root',
// 數據庫密碼
'password' => '',
// 數據庫連接端口
'hostport' => '',
// 數據庫連接參數
'params' => [],
// 數據庫編碼默認采用utf8
'charset' => 'utf8',
// 數據庫表前綴
'prefix' => 'tp_',
],
依次類推
*/
//connect為鏈接數據庫
// $result = Db::connect('db2')->query('select * from sb_ad where ad_id = 1');
// print_r($result);
// $result = Db::connect('db3')->query('select * from sb_ad where ad_id = 1');
// print_r($result);
// $db1 = Db::connect('db1');獲取數據庫對象
// $db2 = Db::connect('db2');獲取數據庫對象然后再操作
// $db1->query('select * from sb_ad where ad_id = 1');
// $db2->query('select * from sb_ad where ad_id = 1');
/*****參數綁定******/
// Db::execute('insert into sb_ad (ad_name, ad_content ,status) values (?, ?, ?)', [3, 'thinkphp', 1]);
// $result = Db::query('select * from sb_ad where ad_id = ?', [3]);
// print_r($result);
/******命名占位符綁定*****/
// Db::execute('insert into sb_ad (ad_name, ad_content ,status) values (:ad_name, :ad_content, :status)', ['ad_name' => 11, 'ad_content' => 'thinkphp', 'status' => 1]);
// $result = Db::query('select * from sb_ad where ad_id=:id', ['id' => 10]);
// print_r($result);
原文鏈接:https://www.jianshu.com/p/7d8a91095eca
相關推薦
- 2023-01-05 Kotlin?掛起函數CPS轉換原理解析_Android
- 2022-12-29 Kotlin使用滾動控件RecyclerView實例教程_Android
- 2022-06-14 Go操作redis與redigo的示例解析_Golang
- 2022-10-19 react-redux多個組件數據共享的方法_React
- 2021-12-13 基于C#實現端口掃描器(單線程和多線程)_C#教程
- 2022-01-17 怎么去操作從后端請求過來的參數
- 2022-10-31 Go語言面試題之select和channel的用法_Golang
- 2022-04-06 聊聊c++數組名稱和sizeof的問題_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支