## 原生查詢
設(shè)置好數(shù)據(jù)庫(kù)連接信息后,我們就可以直接進(jìn)行原生的SQL查詢操作了,包括`query`和`execute`兩個(gè)方法,分別用于查詢操作和寫操作,下面我們來(lái)實(shí)現(xiàn)數(shù)據(jù)表`think_data`的CURD操作。
### 創(chuàng)建(create)
~~~
// 插入記錄
$result = Db::execute('insert into think_data (id, name ,status) values (5, "thinkphp",1)');
dump($result);
~~~
### 更新(update)
~~~
// 更新記錄
$result = Db::execute('update think_data set name = "framework" where id = 5 ');
dump($result);
~~~
### 讀?。╮ead)
~~~
// 查詢數(shù)據(jù)
$result = Db::query('select * from think_data where id = 5');
dump($result);
~~~
> query方法返回的結(jié)果是一個(gè)數(shù)據(jù)集(數(shù)組),如果沒(méi)有查詢到數(shù)據(jù)則返回空數(shù)組。
### 刪除(delete)
~~~
// 刪除數(shù)據(jù)
$result = Db::execute('delete from think_data where id = 5 ');
dump($result);
~~~
### 其它操作
可以執(zhí)行一些其他的數(shù)據(jù)庫(kù)操作,原則上,讀操作都使用`query`方法,寫操作使用`execute`方法即可,例如:
~~~
// 顯示數(shù)據(jù)庫(kù)列表
$result = Db::query('show tables from demo');
dump($result);
// 清空數(shù)據(jù)表
$result = Db::execute('TRUNCATE table think_data');
dump($result);
~~~
> `query`方法用于查詢,默認(rèn)情況下返回的是數(shù)據(jù)集(二維數(shù)組),`execute`方法的返回值是影響的記錄數(shù)。
### 切換數(shù)據(jù)庫(kù)
在進(jìn)行數(shù)據(jù)庫(kù)查詢的時(shí)候,支持切換數(shù)據(jù)庫(kù)進(jìn)行查詢,例如:
~~~
$result = Db::connect([
// 數(shù)據(jù)庫(kù)類型
'type' => 'mysql',
// 服務(wù)器地址
'hostname' => '127.0.0.1',
// 數(shù)據(jù)庫(kù)名
'database' => 'thinkphp',
// 數(shù)據(jù)庫(kù)用戶名
'username' => 'root',
// 數(shù)據(jù)庫(kù)密碼
'password' => '123456',
// 數(shù)據(jù)庫(kù)連接端口
'hostport' => '',
// 數(shù)據(jù)庫(kù)連接參數(shù)
'params' => [],
// 數(shù)據(jù)庫(kù)編碼默認(rèn)采用utf8
'charset' => 'utf8',
// 數(shù)據(jù)庫(kù)表前綴
'prefix' => 'think_',
])->query('select * from think_data');
dump($result);
~~~
或者采用字符串方式定義(字符串方式無(wú)法定義數(shù)據(jù)表前綴和連接參數(shù)),如下:
~~~
$result = Db::connect('mysql://root:123456@127.0.0.1:3306/thinkphp#utf8')->query('select * from think_data where id = 1');
dump($result);
~~~
為了簡(jiǎn)化代碼,通常的做法是事先在配置文件中定義好多個(gè)數(shù)據(jù)庫(kù)的連接配置,例如,我們?cè)趹?yīng)用配置文件(`application/config.php`)中添加配置如下:
~~~
// 數(shù)據(jù)庫(kù)配置1
'db1' => [
// 數(shù)據(jù)庫(kù)類型
'type' => 'mysql',
// 服務(wù)器地址
'hostname' => '127.0.0.1',
// 數(shù)據(jù)庫(kù)名
'database' => 'thinkphp',
// 數(shù)據(jù)庫(kù)用戶名
'username' => 'root',
// 數(shù)據(jù)庫(kù)密碼
'password' => '123456',
// 數(shù)據(jù)庫(kù)連接端口
'hostport' => '',
// 數(shù)據(jù)庫(kù)連接參數(shù)
'params' => [],
// 數(shù)據(jù)庫(kù)編碼默認(rèn)采用utf8
'charset' => 'utf8',
// 數(shù)據(jù)庫(kù)表前綴
'prefix' => 'think_',
],
// 數(shù)據(jù)庫(kù)配置2
'db2' => [
// 數(shù)據(jù)庫(kù)類型
'type' => 'mysql',
// 服務(wù)器地址
'hostname' => '127.0.0.1',
// 數(shù)據(jù)庫(kù)名
'database' => 'test',
// 數(shù)據(jù)庫(kù)用戶名
'username' => 'root',
// 數(shù)據(jù)庫(kù)密碼
'password' => '',
// 數(shù)據(jù)庫(kù)連接端口
'hostport' => '',
// 數(shù)據(jù)庫(kù)連接參數(shù)
'params' => [],
// 數(shù)據(jù)庫(kù)編碼默認(rèn)采用utf8
'charset' => 'utf8',
// 數(shù)據(jù)庫(kù)表前綴
'prefix' => 'test_',
],
~~~
然后就可以直接在`connect`方法中傳入配置參數(shù)進(jìn)行切換數(shù)據(jù)庫(kù)連接,例如:
~~~
$result = Db::connect('db1')->query('select * from think_data where id = 1');
$result = Db::connect('db2')->query('select * from think_data where id = 1');
~~~
`connect`方法中的配置參數(shù)需要完整定義,并且僅僅對(duì)當(dāng)此查詢有效,下次調(diào)用`Db`類的時(shí)候還是使用默認(rèn)的數(shù)據(jù)庫(kù)連接。如果需要多次切換數(shù)據(jù)庫(kù)查詢,可以使用:
~~~
$db1 = Db::connect('db1');
$db2 = Db::connect('db2');
$db1->query('select * from think_data where id = 1');
$db2->query('select * from think_data where id = 1');
~~~
>[danger] connect方法必須直接進(jìn)行查詢,下面的寫法是無(wú)效的
> ~~~
> Db::connect('db1');
> Db::query('select * from think_data where id = 1');
> ~~~
### 參數(shù)綁定
實(shí)際開(kāi)發(fā)中,可能某些數(shù)據(jù)使用的是外部傳入的變量,為了讓查詢操作更加安全,我們建議使用參數(shù)綁定機(jī)制,例如上面的操作可以改為:
~~~
Db::execute('insert into think_data (id, name ,status) values (?, ?, ?)', [8, 'thinkphp', 1]);
$result = Db::query('select * from think_data where id = ?', [8]);
dump($result);
~~~
也支持命名占位符綁定,例如:
~~~
Db::execute('insert into think_data (id, name , status) values (:id, :name, :status)', ['id' => 10, 'name' => 'thinkphp', 'status' => 1]);
$result = Db::query('select * from think_data where id=:id', ['id' => 10]);
dump($result);
~~~
- 零、序言
- 一、基礎(chǔ)
- (1)簡(jiǎn)介
- (2)安裝
- (3)目錄結(jié)構(gòu)
- (4)運(yùn)行環(huán)境
- (5)入口文件
- (6)資源訪問(wèn)
- (7)調(diào)試模式
- (8)控制器
- (9)視圖
- (10)讀取數(shù)據(jù)
- (11)總結(jié)
- 二、URL和路由
- (1)URL訪問(wèn)
- (2)參數(shù)傳入
- (3)隱藏入口
- (4)定義路由
- (5)URL生成
- (6)總結(jié)
- 三、請(qǐng)求和響應(yīng)
- (1)請(qǐng)求對(duì)象
- (2)請(qǐng)求信息
- (3)響應(yīng)對(duì)象
- (4)總結(jié)
- 四、數(shù)據(jù)庫(kù)
- (1)準(zhǔn)備
- (2)數(shù)據(jù)庫(kù)配置
- (3)原生查詢
- (4)查詢構(gòu)造器
- (5)鏈?zhǔn)讲僮?/a>
- (6)事務(wù)支持
- 五、查詢語(yǔ)言
- (1)查詢表達(dá)式
- (2)批量查詢
- (3)快捷查詢
- (4)視圖查詢
- (5)閉包查詢
- (6)獲取值和列
- (7)聚合查詢
- (8)時(shí)間查詢
- (9)字符串查詢
- (10)分塊查詢
- 六、模型和關(guān)聯(lián)
- (1)模型定義
- (2)基礎(chǔ)操作
- (3)讀取器和修改器
- (4)類型轉(zhuǎn)換和自動(dòng)完成
- (5)查詢范圍
- (6)輸入和驗(yàn)證
- (7)關(guān)聯(lián)
- (8)模型輸出
- 七、視圖和模板
- (1)模板輸出
- (2)分頁(yè)輸出
- (3)公共模板
- (4)模板定位
- (5)布局模板
- (6)標(biāo)簽定制
- (7)輸出替換
- (8)渲染內(nèi)容
- (9)助手函數(shù)
- 八、調(diào)試和日志
- (1)第一式:未雨綢繆——頁(yè)面Trace
- (2)第二式:初見(jiàn)端倪——異常頁(yè)面
- (3)第三式:撥云見(jiàn)日——斷點(diǎn)調(diào)試
- (4)第四式:欲窮千里——日志分析
- (5)第五式:運(yùn)籌帷幄——遠(yuǎn)程調(diào)試
- 九、API開(kāi)發(fā)
- (1)API版本
- (2)異常處理
- (3)RESTFul
- (4)REST調(diào)試
- (5)API調(diào)試
- (6)安全建議
- 十、命令行工具
- (1)查看指令
- (2)模塊生成
- (3)控制器生成
- (4)生成類庫(kù)映射文件
- (5)生成路由緩存
- (6)生成字段緩存
- (7)指令擴(kuò)展
- (8)命令行調(diào)試
- (9)命令行顏色支持
- (10)命令調(diào)用
- 十一、擴(kuò)展
- (1)函數(shù)擴(kuò)展
- (2)類庫(kù)擴(kuò)展
- (3)驅(qū)動(dòng)擴(kuò)展
- (4)Composer擴(kuò)展
- 十二、雜項(xiàng)
- Session
- Cookie
- 驗(yàn)證碼
- 文件上傳
- 圖像處理
- 單元測(cè)試
- 番外篇:學(xué)習(xí)ThinkPHP5的正確姿勢(shì)
- 概念篇:ThinkPHP5名詞解釋
- 附錄A、常見(jiàn)問(wèn)題集
- 附錄B、3.2和5.0區(qū)別
- 附錄C、助手函數(shù)
- 附錄D、5.1你必須努力避免的一些問(wèn)題