我做的一个数据库的增删改查

<?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.'记录':'添加失败';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值