返回查询构造器中常......登陆

查询构造器中常用的CURD操作

肖凌2019-10-28 13:57:3320

<?php

        namespace app\index\controller;

        use think\Db;

        class Index

        {

                    public function index()

                        {

                                return ;

                        }

        

                    //单条查询

                    public function find(){

                            $res = Db::table('reader')

                            ->where('id','>',1) //id大于1

                            ->find();

                            dump($res);

                    }

        

            //多条查询方法

                    public function select(){

                                $res = Db::table('reader')

                                //指定字段

                                ->field(['name','age','tel','email'])

                                //->field(['name'=>'姓名','age'=>'年龄','tel'=>'电话','email'=>'邮箱'])

                                 ->where('id','>=','2')

                                 ->order('name','DESC')

                                 ->select();

                             dump($res);

                 }

        

                    //新增记录

                    public function insert(){

                            //添加一条记录

                            $data =['name'=>'孙一','age'=>32,'tel'=>'15926897575','email'=>'15926897575@qq.com','salary'=>3500];

                        

                            $res = Db::table('reader')

                            ->data($data)

                            ->insert();

                             echo $res ? '添加'.$res.'条记录成功!' :'添加失败';

                    }

        

                    //新增多条记录

                    public function insertAll(){

                            //添加多条记录

                            $data =[

                            ['name'=>'胡中','age'=>36,'tel'=>'13586892126','email'=>'13586892126@qq.com','salary'=>3500],

                            ['name'=>'长国','age'=>21,'tel'=>'15895656563','email'=>'15895656563@qq.com','salary'=>3500],

                            ['name'=>'至国','age'=>38,'tel'=>'13698366535','email'=>'13698366535@qq.com','salary'=>3500],

                               ];

                               $res = Db::table('reader')

                            ->data($data)

                            ->insertAll();

                             echo $res ? '添加'.$res.'条记录成功!' :'添加失败';   

                    }

        

                    public function update(){

                            //更新记录操作

                           $res = Db::table('reader')

                                    ->data(['salary'=>Db::raw('salary+500')])

                                    ->where('salary','<=',3000)

                                    ->update();

                              echo $res ? '更新'.$res.'条记录成功!' :'更新失败';  

                    }

        

                    public function delete(){

                            //删除记录

                            $res = Db::table('reader')

                            ->where('salary','>=',4000)

                            ->delete();

                           echo $res ? '删除'.$res.'条记录成功!' :'删除失败';     

                    }

        

        }


最新手记推荐

• 依赖注入小例子• 模板显示用户信息表,并分页显示• 模板继承,模仿php页面• 模拟用户登录验证的案例 依赖注入 Facade• 创建一个验证器, 用来对模型对应的表中字段进行验证处理

全部回复(0)我要回复

暂无评论~
  • 取消回复发送
  • PHP中文网