Blogger Information
Blog 8
fans 0
comment 0
visits 7322
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5.23 查询构造器中的10个最常用的方法
Allen的php博客
Original
656 people have browsed it

原生查询和构造查器询都可实现查询功能,原生查询包括:通用占位符和命名占位符查询,通用占位符查询使用不灵活,查询条件顺序不能出错,而命名占位符查询试用 :参数代替通用占位符的?,可打乱顺序,更加方便试用。

构造器查询10大查询方法:table():  指定查询数据表、field():  指定查询字段、where():  指定查询条件、order():  指定结果排序、limit():  指定查询结果数、 find():   查询一条记录、 select(): 查询数据集、 insert(): 写入数据、 update(): 更新数据、delete(): 删除数据,使用更加多变。

原生查询实例:

<?php

namespace app\admin\controller;

use think\Db;


class Admin

{

    public function index()

    {

        return '网站后台用户控制器';

    }


    public function select()

    {

    //测试数据库是否连接成功

    // dump(\think\Db::query("select * from staff"));

    // 查询:不仅仅是读,还包括写,CURD:增删改查

    // 原生查询:只用到连接器Connection中的query()读操作:select

    // execute()专用于写操作:新增、更新、删除

   

    //查询staff表,salary大于5000的员工信息

    // $sql = "SELECT name,salary FROM staff WHERE salary>5000 LIMIT 5";

    // $res = Db::query($sql);

    // dump($res);


    //上面方法,非常不安全,为了防止sql注入,使用下面方法:

    //原生查询方法:1、通用占位符 ---这种方式也不好,占位符顺序不能错

    // $sql = "SELECT name,salary FROM staff WHERE salary>? LIMIT ?";

    // $res = Db::query($sql,[5000,5]);

    // dump($res);


    //原生查询方法:2、命名占位符 

    $sql = "SELECT name,salary FROM staff WHERE salary>:salary LIMIT :num";

    $res = Db::query($sql,['num'=>[5,\PDO::PARAM_INT],'salary'=>[5000,\PDO::PARAM_INT]]);

    dump($res);


    }


    public function update()//原生写操作

    {

    //将id=10的员工,工资修改为6500

    $sql = "UPDATE staff SET salary = :salary WHERE staff_id = :staff_id";

    DB::execute($sql,['staff_id'=>[10,\PDO::PARAM_INT],[6500,\PDO::PARAM_INT]]);

    return "更新成功!";//如果操作失败,会自动终止并抛出异常

   

    }


}

构造查器询实例:

<?php

namespace app\admin\controller;

use think\Db;


class Query

{

//读操作返回的都是二维数组,没有满足条件的记录,返回的是一个空数组

//写操作返回的是受影响的记录数量,如果没有返回0

public function find()

{

//查询单条数据

$res = DB::table('staff')

->field(['name'=>'姓名','sex'=>'性别','salary'=>'工资'])

->where('staff_id','=',10) //where(字段,表达式,条件)

->find();

dump($res);

}


//查询多条记录

public function select()

{

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

->field(['name'=>'姓名','sex'=>'性别','salary'=>'工资'])

->where('salary','>',5000)

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

->limit(1)

->select();

dump($res);

}


//新增单挑记录:insert()

public function insert()

{


$data = [

'name'=>'茅十八',

'sex'=>0,

'age'=>28,

'salary'=>4500

];

// $num = Db::table('staff')->insert($data);

// $id = Db::getLastInsID();

// return $num ? '添加成功,id='.$id : '添加失败!没有添加数据记录';


//以上方法还有更简化的操作,insert()、select()、find()等方法已经是最终方法了,推荐不要在该方法中带参数

//data($data):将要处理的数据打包

//insertGetId() == insert() + getLastInsID()

$num = Db::table('staff')->data($data)->insert();

$id = Db::getLastInsID();

return $num ? '添加成功,id='.$id : '添加失败!没有添加数据记录';

}


//新增多条记录:insertAll()

public function inserts()

{

$data = [

['name'=>'刘备','sex'=>0,'salary'=>7000],

['name'=>'关羽','sex'=>0,'salary'=>6000],

['name'=>'张飞','sex'=>0,'salary'=>5000]

];

$num = Db::table('staff')->data($data)->insertAll();

return $num ? $num.'条记录添加成功!' : '添加失败!没有数据记录';

}


//更新操作

public function update()

{

//更新操作必须是基于前置查询,不允许无条件更新

//更新试用update(),也是一个终极方法


//把工资超过9000的员工,工资减500

// $num = Db::table('staff')

// ->where('salary','>=',9000)

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

// ->update();

// return $num ? $num.'条记录更新成功!' : '更新失败!没有添加数据记录';


//直接为某个条件,执行更新操作方法

//id为10的,年龄更新为30岁

$num = Db::table('staff')->update(['age'=>30,'staff_id'=>10]);

return $num ? $num.'条记录更新成功!' : '更新失败!没有数据记录';

}


//删除操作

public function delete()

{

//删除也必须基于前置查询,不允许无条件删除

//删除用的是delete()


$num = Db::table('staff')

->where('staff_id','=',10)//删除条件

->delete();

//表清空操作

// $num = Db::table('staff')->delete(true);

return $num ? $num.'条记录删除成功!' : '更新失败!没有删除数据记录';

}


/*

提醒: 

1. 删除数据是非常危险的操作,强烈建议使用框架提供的软删除来实现,即用更新来模拟删除

2. 在实际开发过程中,尽可能避免直接在控制器进行数据库操作,而是使用模型来实现

*/



}


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!