<?php namespace app\index\controller; //use think\Controller; use think\Db; class Index { // http://scj.shopproject.com/index.php/index/index/index // public function index() // { // return $this->fetch(); // } // http://scj.shopproject.com/index.php/index/index/hello public function hello() { return 'hello'; } // http://scj.shopproject.com/index.php/index/index/config public function config() { return config('site_name'); } // http://scj.shopproject.com/index.php/index/index/configClass public function configClass() { dump(\think\Config::get()); } // http://scj.shopproject.com/index.php/index/index/demo public function demo(){ // 获取数据库的链接实力 $link=Db::connect(); // 利用链接实力调研查询方法 $result=$link->table('s_product')->select(); // shuju dump($result); } // http://scj.shopproject.com/index.php/index/index/demo1 public function demo1(){ $config=[ 'type' => 'mysql', 'hostname' => '127.0.0.1', // 数据库名 'database' => 'scjshop', // 用户名 'username' => 'root', // 密码 'password' => 'root', // 端口 'hostport' => '3306' ]; $link=Db::connect($config); $result=$link->table('s_product')->select(); // shuju dump($result); } public function select(){ // 条件查询 // $sql='SELECT * FROM `s_user` WHERE gender =?'; $sql='SELECT * FROM `s_user` WHERE gender =:gender'; // $result=Db::query($sql,[1]); $result=Db::query($sql,['gender'=>1]); dump($result); } public function update(){ // 条件查询 $sql='update s_user set gender=:gender where id=3'; // $result=Db::query($sql,[1]); $result=Db::execute($sql,['gender'=>1]); dump($result); } public function insert(){ // 条件查询 $sql='INSERT INTO `s_user`(`id`, `name`, `phone`, `gender`) VALUES (5,"华卓","13212341234",0)'; // $result=Db::query($sql,[1]); $result=Db::execute($sql); dump($result); } public function table(){ // table方法 field dump( Db::table('s_user')->field(['name'])->where(['id'=>['>',1],'gender'=>['>',0]])->select() ); } // 闭包查询 public function callback(){ // table方法 field $gender=0; dump( Db::table('s_user')->field(['name'])->where(function ($query) use ($gender){ $query->where('id','>',1) ->where('gender','>',$gender); })->select() ); } // public function callback1(){ // table方法 field $gender=0; dump( Db::select(function ($query) use ($gender){ $query->table('s_user')->field(['name'=>'姓名','gender'=>'性别'])->where(['id'=>['>',1],'gender'=>['>',$gender]]); }) ); } // 添加数据 public function insert_data(){ // table方法 field $result=Db::table('s_user')->insert(['id'=>'6','name'=>'史晨洁','phone'=>'13212341234','gender'=>'1']); return $result?'成功添加了'.$result.'记录':'添加失败'; } // 添加数据 public function insert_all(){ // table方法 field $result=Db::table('s_user')->insertAll([ ['id'=>'6','name'=>'史晨洁','phone'=>'13212341234','gender'=>'0'], ['id'=>'8','name'=>'张三分','phone'=>'13212341234','gender'=>'1'], ['id'=>'9','name'=>'赵明','phone'=>'13212341234','gender'=>'0'] ]); return $result?'成功添加了'.$result.'记录':'添加失败'; } // 跟新数据 public function update_data(){ $result=Db::table('s_user')->where('id', 6)->update(['gender'=>'1' ]); return $result?'成功跟新了'.$result.'记录':'添加失败'; } // 自增数据 public function auto_add(){ $result=Db::table('s_user')->where('gender', 1)->setInc('id',1); return $result?'成功跟新了'.$result.'记录':'添加失败'; } }
我做的一个数据库的增删改查
最新推荐文章于 2022-10-01 17:29:04 发布