博主信息
笑颜常开的博客
博文
61
粉丝
0
评论
0
访问量
28040
积分:0
P豆:211.5

thinkphp5.1查询构造器

2019年04月17日 19:28:26阅读数:566博客 / 笑颜常开的博客/ PHP开发

<?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举报处理!

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论
  • mysql的方法:1、修改配置文件,在【my.ini】增加相关代码,定义慢时间以及慢log日志记录;2、通过MySQL数据库开启慢
    使用EXPLAIN关键字可以模拟优化执行SQL语句,从而知道MySQL是如何处理你的SQL语句的。分析你的语句或是表结的性能瓶颈。
    在数据库物理设计阶段,为数据表创建索引的目的是:提高的检索能力、提高效率。
    SQL就是同各种数据库建立联系,进行沟通,其特点有:1、真正的客户机/服务体系结;2、图形化用户界面,使系统管理和数据库管理更加直观、简单;3、丰富的编程接口工具,为用户进行程序设计提供了更大的选择余地
    MySQL 体系架主要分为两部分:客户端和服务端客户端客户端连接主要负责一些客户端的连接,针对不同的编程语言驱动提供连接服务。