zoukankan      html  css  js  c++  java
  • 模型类:连接数据库

    1.每个方法都要连接数据库,解决办法:把重复代码提取到基础模型类,封装成i一个方法initDao---------》在构造方法里面实例化------------》将结果保存到基础模型类的$dao属性里面---------》继承基础模型类

    /**
     * 基础模型类
     */
        class DaoModel{
            //存储数据库操作对象
            protected $dao;
            //初始化Dao
            protected function initDao(){
            // 加载MySQLDB类文件
            include './MySQLDB.class.php';
            $config = array(
                'pass'=>'zhouyang',
                'dbname'=>'php2017'
            );
            $this->dao = MySQLDB::getInstance($config);
            }
            /**
             * 构造函数
             */
            public function __construct(){
                $this->initDao();
            }
    
        }
    /**
     * php_student表的操作模型
     */
    //加载基础模型类
    include './Model.class.php';
    //继承基础模型类
    class StuModel extends Model{
        /**
         * 获得学生列表
         */
        public function getList() {
            $sql = "select * from php_student";
            return $this->dao->fetchAll($sql); // 返回二维数组
        }
        /**
         * 根据id号删除一条学生记录
         */
        public function del($stu_id) {
            $sql = "delete from php_student where id=$stu_id";
            return $this->dao->my_query($sql);
        }
    }
  • 相关阅读:
    grpc(五)
    go的代码库
    grpc(四)
    grpc(三)
    grpc(二)
    grpc(一)
    java的泛型
    如何学习编程语言
    老男孩K8S集群部署(二)
    VMware虚拟机状态正常,但SecureCRT连接时显示超时的解决方法
  • 原文地址:https://www.cnblogs.com/xiaobiaomei/p/8325916.html
Copyright © 2011-2022 走看看