abstract:<?php/** * Created by PhpStorm. * User: Administrator * Date: 2019/5/27 * Time: 14:00 */namespace app\index\controller;use think\Db;class Query{ //读操作返回的都是二维数组,没有满足条件的记录,返回的是一个空数组 &nbs
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/5/27
* Time: 14:00
*/
namespace app\index\controller;
use think\Db;
class Query
{
//读操作返回的都是二维数组,没有满足条件的记录,返回的是一个空数组
//写操作返回的是受影响的记录数量,如果没有返回0
public function find()
{
//查询单条数据
$res = Db::table("goods_spu")->find();
dump($res);
}
public function select()
{
//查询满足条件的多条记录
$res = Db::table("goods_spu")
->where('id','>',1)
->select();
dump($res);
}
public function insert()
{
//新增数据单条记录:insert()
$data = [
//商品编号,唯一
'spu_no' => '20190527001',
//商品名称
'goods_name'=>'华为',
//最低售价
'low_price'=>1000,
//分类id
'category_id'=>'1',
//品牌id
'brand_id'=>'2',
];
//$res = Db::table("goods_spu")->insert($data);
$res = Db::table("goods_spu")->data($data)->insert();
}
public function insertAll()
{
$data = [
['spu_no' => '20190527002', 'goods_name'=>'华为', 'low_price'=>1000, 'category_id'=>'1', 'brand_id'=>'2'],
['spu_no' => '20190527003', 'goods_name'=>'小米', 'low_price'=>2000, 'category_id'=>'1', 'brand_id'=>'2'],
['spu_no' => '20190527004', 'goods_name'=>'oppo', 'low_price'=>3000, 'category_id'=>'1', 'brand_id'=>'2']
];
$res = Db::table("goods_spu")->insertAll($data);
}
public function update()
{
//更新操作必须是基于前置条件,不允许无条件更新
//跟新使用uopdate(),也是一个终极方法
Db::table('goods_spu')
->where('low_price','<','4000')
->data(['low_price'=>Db::raw('low_price+1000')])
->update();
}
public function delete()
{
//删除也必须基于前置查询,不允许无条件删除
//删除用的是delete()
Db::table('goods_spu')->where('id','1')-delete();
}
}
Correcting teacher:天蓬老师Correction time:2019-05-28 16:02:23
Teacher's summary:写得不错, 对于数据库的基本操作, 常用 的就是这些了