前言
注意,tp6在進(jìn)行語法學(xué)習(xí)的時(shí)候都是在app/index.php中寫代碼的,代碼寫在index函數(shù)下面,而且
tp6自帶的文件都是由自動(dòng)加載器的,不需要包含autoload.php文件
1. 數(shù)據(jù)庫配置
要對(duì)數(shù)據(jù)庫進(jìn)行操作,要修改兩個(gè)地方,一個(gè)數(shù).env文件,一個(gè)是config/database.php文件
- config/database.php
'connections' => [
'mysql' => [ // 代表連接的一個(gè)數(shù)據(jù)庫
// 數(shù)據(jù)庫類型
'type' => env('database.type', 'mysql'),
// 服務(wù)器地址
'hostname' => env('database.hostname', '127.0.0.1'),
// 數(shù)據(jù)庫名
'database' => env('database.database', 'cr'),
// 用戶名
'username' => env('database.username', 'root'),
// 密碼
'password' => env('database.password', '901026'),
// 端口
'hostport' => env('database.hostport', '3306'),
// 數(shù)據(jù)庫連接參數(shù)
'params' => [],
// 數(shù)據(jù)庫編碼默認(rèn)采用utf8
'charset' => env('database.charset', 'utf8'),
// 數(shù)據(jù)庫表前綴 配置后只有寫數(shù)據(jù)庫后面
'prefix' => env('database.prefix', ''),
// 數(shù)據(jù)庫部署方式:0 集中式(單一服務(wù)器),1 分布式(主從服務(wù)器)
'deploy' => 0,
// 數(shù)據(jù)庫讀寫是否分離 主從式有效
'rw_separate' => false,
// 讀寫分離后 主服務(wù)器數(shù)量
'master_num' => 1,
// 指定從服務(wù)器序號(hào)
'slave_no' => '',
// 是否嚴(yán)格檢查字段是否存在
'fields_strict' => true,
// 是否需要斷線重連
'break_reconnect' => false,
// 監(jiān)聽SQL
'trigger_sql' => env('app_debug', true),
// 開啟字段緩存
'fields_cache' => false,
],
],
connections里面的一個(gè)子元素代表連接的一個(gè)數(shù)據(jù)庫,要使用多個(gè)數(shù)據(jù)庫,要添加多個(gè)數(shù)據(jù)庫對(duì)象,寫
prefix
之后就只需要寫數(shù)據(jù)表后面的字段了
- config/env
[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = cr
USERNAME = root
PASSWORD = 901026
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true
注意和config/database.php對(duì)應(yīng)上就可以了
2. 數(shù)據(jù)庫操作
創(chuàng)建數(shù)據(jù)庫
create database cr;
use cr;
insert into phpcn_user values (1, '歐陽克'),(2, '朱'),(3,'asd'),(4,'asde'),(5,'rts');
在寫操作數(shù)據(jù)庫語句之前寫
use think\facade\Db;
1. 查詢操作
-
find():通過主鍵查找,find方法查詢結(jié)果不存在,返回 null,否則返回結(jié)果數(shù)組
-
where():查詢的條件
-
field(), column():查詢某一列數(shù)據(jù)
-
value() :通過值查找,查詢某段的值
如上都是查詢一條數(shù)據(jù),一列數(shù)據(jù),或者是一個(gè)數(shù)據(jù)
- select() :查詢好多條數(shù)據(jù),和toArray()一起使用
$result = Db::table('phpcn_user')->column('id');
printf('<pre>%s</pre>',print_r($result,true));
$result = Db::table('phpcn_user')->where('id','>','2')->value('name');
printf('<pre>%s</pre>', print_r($result, true));
$result = Db::table('phpcn_user')->where('id', '3')->value('name');
printf('<pre>%s</pre>', print_r($result, true));
print('-----------------');
$result = Db::table('phpcn_user')->where('id','>=','2')->select()->toArray();
printf('<pre>%s</pre>', print_r($result, true));
2. 插入操作
- insert($data) $data為關(guān)聯(lián)數(shù)組,通常通過
- insertAll($data) :添加多條數(shù)據(jù)
- insertGetId($data) :添加數(shù)據(jù)并且獲取添加數(shù)據(jù)的主鍵
$data = ['id'=>11,'name'=>'zxasdc'];
$result = DB::table('phpcn_user')->insert($data);
echo $result;
$data = ['id'=>11,'name'=>'zxasdc'];
$result = DB::table('phpcn_user')->insertGetId($data);
echo $result;
$data = [
[
'id'=>7,
'name'=>'dfg'
],
[
'id' => 8,
'name' => 'df8g'
]
];
$result = DB::table('phpcn_user')->insertAll($data);
echo $result;
插入操作,插入的都是關(guān)聯(lián)數(shù)組
3. 修改
- update(修改后的值) + where(修改哪一個(gè))
- updata($data) $data為關(guān)聯(lián)數(shù)組
- inc():自增 dec():自減
$result = Db::table('phpcn_user')->where('id',3)->update(['name'=>'ykk']);
4. 刪除
- delete()
- useSoftDelete()
$result = Db::table('phpcn_user')->where('id',5)->delete();
5. 其他
- save($data) : $data是關(guān)聯(lián)數(shù)組,save方法統(tǒng)一寫入數(shù)據(jù),自動(dòng)判斷是新增還是更新數(shù)據(jù)(以寫入數(shù)據(jù)中是否存在主鍵數(shù)據(jù)為依據(jù))
- query() :方法用于執(zhí)行 MySql 查詢操作
- execute() : 方法用于執(zhí)行 MySql 新增和修改操作
2和3都是直接操作數(shù)據(jù)庫的文章來源:http://www.zghlxwxcb.cn/news/detail-427602.html
$result = Db::query("select * from `phpcn_user` where `id` > 3");
print_r($result);
$result = Db::execute("insert into `phpcn_user` values (11, 'asd')");
print_r($result);
3.數(shù)據(jù)集
- toArray()
- isEmpty()
總結(jié)
數(shù)據(jù)庫操作前一定要加上
use think\facade\Db;
文章來源地址http://www.zghlxwxcb.cn/news/detail-427602.html
到了這里,關(guān)于ThinkPHP6之?dāng)?shù)據(jù)庫操作上的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!