徐膛纽会碾魄刷嚼记武端绞黎
数据表结构
查询id=1的用户的姓名
<?php namespace app\index\controller; use think\Db; class Index { public function index() { //value('字段','默认值') $result=Db::table('staff')->where('id=1')->value('name'); dump($result); } }
执行:
查询id>10的用户姓名
<?php namespace app\index\controller; use think\Db; class Index { public function index() { //column('字段','字段') $result=Db::table('staff') ->where('id','>',10) ->column('name'); dump($result); } }
执行:
column()传入2个参数,第一个参数作为值,第二个参数作为键
<?php namespace app\index\controller; use think\Db; class Index { public function index() { //column('字段','字段') $result=Db::table('staff') ->where('id','>',10) ->column('age','name'); dump($result); } }
执行:
delete(),必须给出删除条件,且不支持闭包
删除id=11张无忌
<?php namespace app\index\controller; use think\Db; class Index { public function index() { $affected=Db::table('staff') ->delete(11);//直接根据主键id删除 return $affected ? "成功删除了".$affected."条记录":"没有记录被删除"; } }
执行:
张无忌被删除
向表中添加数据:
如果需要删除id=14,15的员工,可以这样写
<?php namespace app\index\controller; use think\Db; class Index { public function index() { $affected=Db::table('staff') ->delete([14,15]);//直接根据主键id删除 return $affected ? "成功删除了".$affected."条记录":"没有记录被删除"; } }
执行:
如果需要删除整张表,可以这样写(这是非常危险的操作,慎重!!!)
<?php namespace app\index\controller; use think\Db; class Index { public function index() { $affected=Db::table('staff') ->delete(true);//删除整张表 return $affected ? "成功删除了".$affected."条记录":"没有记录被删除"; } }
盎才巴权湃捻嫂踌皋任煌烁亭