<?phpnamespace app/index/controller;use think/Db;class Demo{//1、全局配置 config/database.php配置public function dbTest(){return Db::table('pzq_article')->where('id','29')->value('title');}//2、动态配置 think/db/Query.php中有一个方法connect()public function dbTest2(){return Db::connect(['type'=>'mysql','hostname'=>'localhost','database'=>'top789','username'=>'root','password'=>'root',])->table('pzq_article')->where('id','76')->value('title');}//3、DSN连接public function dbTest3(){$dsn = 'mysql://root:root@localhost:3306/top789#utf8';return Db::connect($dsn)->table('pzq_article')->where('id','88')->value('title');}//4、单条查旬public function dbTest4(){$res = Db::table('pzq_article')->field(['title'=>'标题','id'=>'编号'])//可添加别名->where('id','=',20)//如果是等号,=可以省略->find();//如果是主键查询,可省略上面where,这行写->find(20);dump(is_null($res)?'没有查到':$res);}//5、多条查旬public function dbTest5(){$res = Db::table('pzq_article')->field(['id','cat_id','title'])->where([['id','>',20],['cat_id','=',2],])//一个条件,直接用表达式->where('id','>',20)。多个条件用数组->order('id desc')->limit(3)->select();if(empty($res)){return '没有查到';}else{dump($res);}}//6、数据添加public function dbTest6(){$data = ['name'=>'Sam2','age'=>'29','posttime'=>time()];$dataall=[['name'=>'Sam3','age'=>'29','posttime'=>time()],['name'=>'Sam4','age'=>'30','posttime'=>time()],];//(1)单条插入//return Db::table('test')->data($data)->insert();//(2)插入同时返回新增主键id//return Db::table('test')->insertGetId($data);//(3)插入多条数据return Db::table('test')->data($dataall)->insertAll();}//更新数据public function dbTest7(){// return Db::table('test')// ->where('id','=',4)// ->update(['name'=>'SamC','age'=>'31']);//如果where条件是主键,还可以如下使用return Db::table('test')->update(['name'=>'SamCheng','age'=>'30','id'=>4]);}//删除操作public function dbTest8(){//return Db::table('test')->delete(6);//或者return Db::table('test')->where('id',5)->delete();}//mysql原生语句 查询public function dbTest9(){$sql = "select name,age from test where id>2";dump(Db::query($sql));}//mysql 增、删、改 用Db::execute($sql)public function dbTest10(){//$sql = "update test set name='samC' where id=4";//$sql = "insert test set name='Yan',age='30'";$sql = "delete from test where id=4";return Db::execute($sql);}}