Blogger Information
Blog 8
fans 0
comment 0
visits 5867
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示:查询构造器中的10个最常用的方法-2018年5月23日
往昔流逝的博客
Original
734 people have browsed it

实例演示:查询构造器中的10个最常用的方法

Table(),field(),order(),where(),limit(),insert(),insertAll(),update(),delete(),以及如何获取自增主键和数据打包方法data()

<?php
namespace app\index\controller;
use think\Db;

class Query
{
    //读操作返回的二维数组
    //写操作返回的是受影响的记录数量
    public function find()
    {
        //查询单条记录
        $res = Db::table('users')->field(['name'=>'姓名'])->where('id','>',2)->find();
        dump($res);

        //查询多条记录

        $res = Db::table('users')->order('id,desc')->limit(4)->select();

        dump($res);

    }

    public function insert(){
        //添加一条记录
        $data = ['name'=>'thinkphp','password'=>234];
        $num = Db::table('users')->insert($data);
        return $num ? 'ok,id='.$id :'no';

        //获取自增主键和数据打包方法data()

        $data = ['name'=>'thinkphp','password'=>234];
        $num = Db::table('users')->data($data)->insert();
        $id = Db::getLastInsId();
        return $num ? 'ok,id='.$id :'no';      

         //添加多条记录
        $data = [ ['name'=>'thinkphp','password'=>234],
        ['name'=>'thinkphp1','password'=>1234],
        ];
        $num = Db::table('users')->data($data)->insertAll();
        return $num ? 'ok,num='.$num :'no';
    }
    public function update()
    {
        $num = Db::table('users')->where('id','=','2')->data(['age'=>Db::raw('age+1')])->update();
        return $num ? 'ok,num='.$num :'no';
        
    }
    public function delete(){
        $num = Db::table('users')->where('id','>',20)->delete();
        return $num ? 'ok,num='.$num :'no';
    }
}

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post