Correction status:Uncorrected
Teacher's comments:
thinkphp的数据库操作,使用链式写法。
文件路径:tp/application/index/controller/Test.php
访问其中的方法时,使用 tp.top/index.php/index/test/update,查看执行的结果
<?php namespace app\index\controller; use think\Db; class Test { public function find(){ $res = Db::table('staf') ->field('name,sex,salary') ->where('staff_id',2) ->find(); dump($res); } public function select() { $res=Db::table('staf') ->field('name,sex,salary') ->where('salary','>',3000) ->order('salary','DESC') ->limit(5) ->select(); dump($res); } public function insert() { $data=[ 'name'=>'halc', 'sex'=>1, 'age'=>45, 'salary'=>6800 ]; $num=Db::table('staf')->data($data)->insert(); $id=Db::getLastInsId(); return $num ? '添加,id='.$id : '没有被添加'; } public function insertMulti() { $data=[ ['name'=>'jla','sex'=>0,'age'=>35,'salary'=>7800], ['name'=>'alice','sex'=>0,'age'=>45,'salary'=>8800], ['name'=>'beal','sex'=>1,'age'=>50,'salary'=>9800], ]; $num = DB::table('staf')->data($data)->insertAll(); return $num ? '添加成功'.$num.'条记录':'没有记录添加'; } public function update() { $num = Db::table('staf') ->where('salary','<',3600) ->data(['salary'=>Db::raw('salary+1000')]) ->update(); $num1=Db::table('staf') ->update(['sex'=>1,'staff_id'=>9]); return $num ? '更新成功'.$num.'条记录' :'没有记录更新'; } public function delete() { $num=Db::table('staf')->delete(12); return $num ? '删除了'.$num.'条记录' :'没有删除'; } } ?>
点击 "运行实例" 按钮查看在线实例