(1)配置數(shù)據(jù)庫
先在conf目錄下的database.php里面修改常規(guī)配置信息來重寫配置
查看數(shù)據(jù)庫配置.PNG
切記根目錄設(shè)置的是localhost"8086 / thinkphp / public
(2)Db::table()的四種方法
可以將Db::table()換成Db::name(),后者可以不加表前綴
也可以使用函數(shù)db('city',[],false)->select()來代替使用類的形式
數(shù)據(jù)庫建表信息.PNG
- 得到所有數(shù)據(jù)
Db::table('city')->select();
- 得到給定列的所有數(shù)據(jù)
$res = Db::table('city')->column('city_name','city_pollution');
如果column中存在第二個參數(shù),就用第二個參數(shù)作為數(shù)組的鍵名
兩參數(shù)column打印結(jié)果.PNG
上面兩種方法記錄不存在,將返回空數(shù)據(jù)
- 得到一條數(shù)據(jù)
$res = Db::table('city')->find();
- 返回一條記錄的某個字段值
$res = Db::table('city')->value('city_pollution')
這兩種方法記錄不存在將返回null
四種方法都可以在之間添加where([ 限制條件 ])來進(jìn)行篩選
$res = Db::table('city')->where(['id' => 3])->select();