zoukankan      html  css  js  c++  java
  • 基础模型类

    代码展示

    数据库操作类

    <?php
    class MyPDO
    {
        private static $instance;  //保存对象
        private $host;      //主机地址
        private $dbname;    //数据库名字
        private $port;      //端口
        private $user;      //用户名
        private $pwd;       //密码
        private $charset;   //字符集
        private $link;      //连接对象
    
    
        private function __construct($data){
    
            $this->initParam($data);
            $this->getPDO();
            $this->errorMode();
        }
    
        private function __clone(){
    
        }
    
        //获取单例
        public static function getInstance($data=array()){
            if(!self::$instance instanceof self){
                return self::$instance=new self($data);
            }
            return self::$instance;
        }
    
        //初始化参数
        private function initParam($data){
            $this->host=isset($data['host'])?$data['host']:'localhost'; 
            $this->dbname=isset($data['dbname'])?$data['dbname']:'my_db';
            $this->port=isset($data['port'])?$data['port'] : '3306';
            $this->user=isset($data['user'])?$data['user']:'root';
            $this->pwd=isset($data['pwd'])?$data['pwd']:'root';
            $this->charset=isset($data['charset'])?$data['charset']:'utf8';
        }
    
        //显示错误
        private function showError($e,$sql=null){
            echo "错误信息".$e->getMessage()."<br>";
            echo "错误代码".$e->getCode()."<br>";
            echo "错误文件".$e->getFile()."<br>";
            echo "错误行号".$e->getLine().'<br>';
            if($sql!=null){
                echo "错误sql语句".$sql;
            }
        }
        //连接数据库
        private function getPDO(){
            try {
                $this->link= new PDO("mysql:host={$this->host};port={$this->port};dbname={$this->dbname};charset={$this->charset}","{$this->user}","{$this->pwd}");
            } catch (PDOException $e) {
               $this->showError($e);
                exit;
            }
        }
    
        //设置错误模式
        private function errorMode(){
            $this->link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        }
    
        
    
        /**
         * 增删改功能
         * @param string sql语句
         * @return int 受影响的行数
         */
        public function exec($sql){
            try {
                return $row=$this->link->exec($sql);
            } catch (PDOException $e) {
                $this->showError($e,$sql);
                exit;
            }
    
        }
    
        /**
         * 获取二维数组结果集
         * @param string sql
         * @return PDOStatement 结果集
         */
    
         public function fetchAll($sql){
             try{
                $stmt=$this->link->query($sql);
                return $stmt->fetchAll();
    
             }catch(PDOException $e){
                $this->showError($e,$sql);
             }
    
         }
    
         /**
          * 获取一维数组结果集
          *@param string sql
          *@return PDOStatement 结果集
          */
          public function fetch($sql){
              try {
                $stmt= $this->link->query($sql);
                return $stmt->fetch();
              } catch (PDOException $e) {
                  $this->showError($e,$sql);
              }
    
          }
    
          /**
           * 获取单行单列
           * @param string sql
           * @return mixed 内容
           */
          public  function fetchColumn($sql){
              try {
                $stmt=$this->link->query($sql);
                return $stmt->fetchColumn();
              } catch (PDOException $e) {
                  $this->showError($e,$sql);
              }
          }
    
          //转账事务操作
          public function beginTransction($sql_out,$sql_in){
             try {
                    $this->link->beginTransaction();
                    $out=$this->link->exec($sql_out);
                    $in=$this->link->exec($sql_in);
                    if($out && $in){
                        $this->link->commit();
                        echo "转账成功";
                    }else{
                        $this->link->rollBack();
                    }
             } catch (PDOException $e) {
                 $this->showError($e);
             }
          }
    
    }

    Mybank模型类

    <?php
    //加载Model基础模型类
    require './Model.class.php';
    //转账模型类,有多么功能就封装什么方法就行
    class Mybank extends Moudel
    {
        //基础模型类中 已经继承到方法直接用就行
          //定义转账方法
          public function zhuanZ($out,$in,$money){
              //这里的$this->pdo 就是父类基础模型下的pdo
            return $this->pdo->beginTransction("update my_bank set money=money-$money where carNo=$out","update my_bank set money=money+$money where carNo=$in");
        }
    }

    基础模型类  所有模型类的父类

    <?php
    
        /**
         * 基础模型类,其他模型类继承此类
         * 所有模型类的父类
         */
        class Moudel
        {
            //定义存储pdo对象的属性,要求在子类中可以被访问;
            protected $pdo;
            //构造方法,当子类继承的时候,直接就初始化pdo的对象
            public function __construct(){
    
                //初始化pdo
                $this->initPDO();
            }
    
            //初始化pdo对象
            protected function initPDO(){
                //加载MyPDO类
                require './MyPDO.class.php';
                $this->pdo=MyPDO::getInstance();
            }
          
        }

    Controller 控制器

    <?php 
        //控制器调用视图 view.html
        require 'index.html';
    
        //调用Model 模型类;
        require './Mybank.class.php';
        if(!empty($_GET)){
            $out=$_GET['out'];
            $in=$_GET['in'];
            $money=$_GET['money'];
            //实例化 Mybank 模型类,闭并调用下面的转账方法
            $ban=new Mybank();
            echo $ban->zhuanZ($out,$in,$money);
           
        }

    View 视图

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <form action="" method="get">
            转出卡号:<input type="text" name="out" id=""><br>
            转入卡号:<input type="text" name="in" id=""><br>
            金额:<input type="text" name="money" id=""><br>
            <input type="submit" value="转账">
        </form>
    </body>
    </html>
  • 相关阅读:
    Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7
    CentOS7安装使用MySQL
    使用passenger在Centos7部署Puma+Nginx+Ruby on Rails
    DOS和UNIX文本文件之间相互转换的方法
    CentOS7安装vim7.4
    Python多版本共存之pyenv
    我的Shell + VIM配置
    CentOS7安装Python3.5
    CentOS7系统下搭建Jenkins环境
    Windows系统下搭建Jenkins环境
  • 原文地址:https://www.cnblogs.com/xiaowie/p/14318263.html
Copyright © 2011-2022 走看看