• PhalAPI学习笔记拓展篇 ——— 基于MySQL数据库交互题目


    前言

    公司业务需要转学PHP,而PHP中一个功能强大且生态链完整的PHP接口框架 PhalAPI
    值得大家去学习,本学习笔记持续更新!
    虽然官方文档写的十分明白,以及CSDNPhalAPI框架内容也少之又少。
    因此,以自我踩坑为基础,提供一个更为精简的学习笔记,本学习笔记将会省略部分安装及简单操作。

    拓展篇内容将全面直线进入强化期

    约束

    • 需要满足PhalAPI框架的 ADM(API Domain Model) 模式
    • 内容需全自做,若遇到不会的题目,可在末尾找到相关答案
    • Model查询需要使用NotORM

    基于MySQL数据库交互题目

    自建数据库,字段需求:在这里插入图片描述

    简单篇

    查询某所有的数据

    参考语句 $this->getORM()->select()

    答:………

    单查询id、project、title

    参考语句 $this->getORM()->select()

    答:………

    聚合函数返回总数量

    参考语句 $this->getORM()->select()

    答:………

    查询id为49的数据

    参考语句 $this->getORM()->where()

    答:………

    查询id为49并且title为账号管理的数据

    参考语句 $this->getORM()->where()

    答:………

    中级篇

    查询id为50并且title为账号列表的数据(数组方式)

    参考语句 $array = array(); $this->getORM()->where($array);

    答:………

    查询id为1或title为账号列表的title数据

    参考语句 $this->getORM()->where()

    答:………

    嵌套查询id为51并且title为添加账号

    参考语句 $this->getORM()->where()

    答:………

    嵌套查询id为51并且title为添加账号(关联性数组)

    参考语句 $this->getORM()->where()

    答:………

    高级篇

    in方式查询id为49,50,51的数据

    参考语句 $this->getORM()->where()

    答:………

    in方式查询id为49,50,51和title为账号管理、账号列表、添加账号

    参考语句 $this->getORM()->where()

    答:………

    查询title中包含 账号 的所有数据

    参考语句 $this->getORM()->where()

    答:………

    查询title中为空的所有数据

    参考语句 $this->getORM()->where()

    答:………

    倒序查询id所有数据

    参考语句 $this->getORM()->order()

    答:………

    以id为分组查询所有project字段中数据为 admin的所有数据

    参考语句 $this->getORM()->group()

    答:………

    查询10条数据

    参考语句 $this->getORM()->litmit()

    答:………

    答案

       /**
         *  csdn PhalAPI 数据库交互答案
         *  @Author Marinda
         */
    
    //    初级篇
        public function selectAll(){
            return $this->getORM()->select("*")->fetchAll();
        }
    
        public function selectByIDAndProjectAndTitle(){
            return $this->getORM()->select("id , project ,title")->fetchAll();
        }
    
        public function selectCount(){
    //        另外ORM也封装了一个count函数
    //        return $this->getORM()->count();
            return $this->getORM()->select("COUNT(*)")->fetchAll();
        }
    
        public function selectById(){
            return $this->getORM()->where("id = ?",49)->fetchOne();
        }
    
        public function selectByIdAndTitle(){
            return $this->getORM()->where("(id = ? or title = ?)",49,"账号管理")->fetchOne();
        }
    
    //    中级篇
        public function midSelectByIdAndTitle(){
            $arr = array();
            $arr['id'] = 50;
            $arr['title = ?'] = "账号列表";
            return $this->getORM()->where($arr)->fetchOne();
        }
    
        public function midSelectByIdOrTitle(){
    
            return $this->getORM()->where("id = ?",1)->or("title = ?",'账号列表')->fetchAll();
        }
    
        public function midSelectByIdAndsTitle(){
            $arr = array("51","添加账号");
            return $this->getORM()->where("(id = ? OR title = ?)",$arr)->fetchOne();
        }
    
        public function midSelectByIdRelationAndTitle(){
            $arr = array(":id" => 51,":title","添加账号");
            return $this->getORM()->where("(id = :id OR title = :title)",$arr)->fetchOne();
        }
    
        //    高级篇
        public function highInSelectById(){
            $arr = array(49,50,51);
            return $this->getORM()->where("id",$arr)->fetchAll();
        }
    
    
    //    in方式查询id为49,50,51和title为账号管理、账号列表、添加账号
        public function highInSelectByIdAndTitle(){
            $arr = array(array(49,"账号管理"),array(50,"账号列表"),array(51,"添加账号"));
            return $this->getORM()->where("(id,title)",$arr)->fetchAll();
        }
    
    //    查询title中包含 账号 的所有数据
        public function highLikeTitle(){
            return $this->getORM()->where("title like ?","%账号%")->fetchAll();
        }
    
    //    查询title中为空的所有数据
        public function highTitleNull(){
            return $this->getORM()->where("title is null")->fetchAll();
        }
    
        public function highDescId(){
            return $this->getORM()->order("id desc")->fetchAll();
        }
    
        public function highGroup(){
            return $this->getORM()->group("id","project = 'admin'")->fetchAll();
        }
    
        public function highLimit(){
            return $this->getORM()->limit(10)->fetchAll();
        }
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    结束语

    关于 PhalAPI学习笔记拓展篇 ——— 基于MySQL数据库交互题目 就讲到这里,对你有帮助的话!

    • 点赞
    • 收藏

    谢谢你的观看!

  • 相关阅读:
    【数据结构与算法】第七篇:集合,映射
    京东API接口解析,实现获得JD商品评论
    高伸缩高可用大并发可负载的网站架构
    交互与前端18 CodeMirror 实践2
    【华为正式岗】后台开发1面
    java-net-php-python-ssm仓库管理系统计算机毕业设计程序
    JAVA计算机毕业设计游戏论坛设计Mybatis+源码+数据库+lw文档+系统+调试部署
    消息队列(四):内存管理
    C++中虚表是什么
    猿创征文 | 大数据比赛以及日常开发工具箱
  • 原文地址:https://blog.csdn.net/qq_33638188/article/details/125517531