博主信息
博文 61
粉丝 0
评论 0
访问量 65793
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
thinkphp5.1查询构造器
笑颜常开的博客
原创
1245人浏览过

<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use app\index\model\Staff;
class Test1 extends Controller{
//数据库的连接测试
   public function demo1(){
//        1.静态连接:database.php
       $res=Db::table('staff')->limit(1)->select();
//        2.动态连接:database.php    'my_db'
       $res=Db::connect('my_db')
           ->table('staff')
           ->limit(2)
           ->select();
//        3.环境变量
       $res=Db::table('staff')->limit(3)->select();
//        4.模型链接
       $res=Staff::limit(14)->select();
       dump($res);
   }
//数据库之原生查询:query()
   public function demo2(){
//准备sql语句
       $sql='select `id`,`name`,`age` from `staff` where `age`>:age limit :num;';
//执行查询操作
       $res=Db::query($sql,['age'=>50,'num'=>2]);
       dump($res);
   }
//数据库之原生写操作:execute(),以更新为例
   public function demo3(){
//准备sql语句
       $sql='update `staff` set `salary`=:salary where `id`=:id;';
//执行查询操作
       $res=Db::execute($sql,['salary'=>9000,'id'=>2]);
       dump($res);
   }
// 查询构造器:find()/select()
   public function demo4(){
//fund():返回满足条件的第一条记录
       $res=Db::table('staff')
           ->field('id,name,age')
           ->where('age','>',70)
           ->find();
// 返回满足条件的第一条记录,并且只想获取某一个字段的值:value()
       $res=Db::table('staff')
           ->field('id,name,age')
           ->where('age','>',10)
           ->value('name');
//            select():返回满足条件的所有记录
       $res=Db::table('staff')
           ->field('id,name,age')
           ->where('age','>',70)
           ->select();
//        只想获取到某一列的值:column()
       $res=Db::table('staff')
           ->field('id,name,age')
           ->where('age','>',70)
           ->column('age');
       dump($res);
   }
//查询构造器中的新增操作insert(),insertAll()
   public function demo5(){
//        准备要添加到表中的数据,以数组方式提供
       $data=[
           'name'=>'欧阳锋',
           'sex'=>1,
           'age'=>59,
           'salary'=>8868.58,
           'email'=>'ouyangfeng@php.cn',
           'mobile'=>'15788997652',
           'password'=>sha1('123456'),
           'create_time'=>time(),
           'update_time'=>time(),
           'delete_time'=>0,
       ];
//        执行新增操作,成功会返回新增的数量
       $res=Db::table('staff')->data($data)->insert();
       dump($res);
   }
//查询构造器:update()
   public function demo6(){
//更新条件
       $where['id']=21;
//更新字段
       $data['age']=69;
       $res=Db::table('staff')
           ->where($where)
           ->data($data)
           ->fetchSql(false)
           ->update();
       dump($res);
   }
   public function demo7(){
//更新条件
       $where['id']=12;
//更新字段
//        $data['age']=69;
       $res=Db::table('staff')
           ->fetchSql()
           ->delete($where);
       dump($res);
   }
//    关键字要大写
//如果是等值查询
   public function demo8(){
       $where=[];
   $where['id']=5;
   $res=Db::table('staff')
       ->field('id,name,salary')
       ->where($where)
       ->select();
       dump($res);
   }
}

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学