摘要:1.是在设置别名的时候运行提示不支持的表达式2.是使用limlt时,提示报错limlt不存在<?php namespace app\index\controller; use think\DB; class Demo { public function find() { $res&nbs
1.是在设置别名的时候运行提示不支持的表达式
2.是使用limlt时,提示报错limlt不存在
<?php
namespace app\index\controller;
use think\DB;
class Demo
{
public function find()
{
$res = DB::table('score') //查找符合条件的第一条信息,find()里面的条件为主键
->find(5);
$res = DB::table('score') //也可以这么写 条件写到where里面 效果同上。
->where('id','=',4)
->find();
$res = Db::table('score')
->field(['name','sex','math']) //参数以数组的形式
->where('id',3)
->find();
dump($res);
}
public function select()
{
$res = DB::table('score')
->field(['name','math'])
// ->field(['name'=>'姓名','math'=>'性别']) 如果我设置字段别名 运行的时候报错,说不支持的数据表达式
->where('math','>=',90)
->order('math','DESC')
//->limlt(2) 我的加上这句抛出异常了 说limlt不存在
->select();
dump($res);
}
public function insert()
{
$data =[
'name' => '米希',
'sex' => '1',
'math' => '70',
'java' => '69',
'php' => '64'
];
$num = Db::table('score')->insert($data);
$id = Db::getLastInsID();
return $num ? "id".$id."添加成功" : "添加失败";
$num = Db::table('score')->insertGetId($data); //insertGetId等于insert() + getLastInsID()方法,是一样的效果
return $num ? "id".$id."添加成功" : "添加失败";
$num = $Db::table('score')->data($data)->insert(); //用data打包数据,不要在insert等这类最终方法里写参数
return $num ? "id".$id."添加成功" : "添加失败";
//添加多条记录时
$data =[
['name' => '佩妮','sex' => '1','math' => '60','java' => '69','php' => '54'],
['name' => '谢尔顿','sex' => '0','math' => '99','java' => '97','php' => '91'],
['name' => '莱纳德','sex' => '0','math' => '80','java' => '95','php' => '82']
];
$num = $Db::table('score')->data($data)->insertAll(); //原理和上面一样,只不过要用inserAll()方法
return $num ? "添加成功".$num."记录" : "添加失败";
}
public function update()
{
$num = Db::table('score')
->where('math','<',60)
->data(['math' => Db::raw('math+10')]) //Db::raw()是获取数据库表中原有的记录值
->update();
return $num ? "更新成功".$num."条记录" : "更新失败";
$num = Db::table('score')
->update(['java' => 80,'id' => 5]); //当更新条件为主键时,可以这么写。
return $num ? "更新成功".$num."条记录" : "更新失败";
}
public function delete()
{
$num = Db::table('score') //删除一条记录
->where('math','<',70)
->delete();
return $num ? "删除成功".$num."条记录" : "删除失败";
$num = Db::table('score')->delete(true); //删除全部记录时这么写
return $num ? "删除成功".$num."条记录" : "删除失败";
}
}
?>