PDO_mysql_class

<?php
    header("content-type:text/html;charset=utf-8");
    
    //封装一个类:完成一个new  就可以操作数据库相应操作
    
    class myPDO{
        //定义属性,接受实例化时候传入的数据库信息
        private $type;
        private $host;
        private $port;
        private $dbname;
        private $charset;
        private $user;
        private $pass;

        //pdo属性:将局部变量转换为全局变量
        private $pdo;

        //定义属性,存储一数组属性$args来帮助预处理判断某一个预处理是否已经操作过
        private $args=array();

        //构造方法
        public function __construct($info=array()){
            $this->type = isset($info['type'])?$info['type']:strtolower('mysql');
            $this->host = isset($info['host'])?$info['host']:'host';
            $this->port = isset($info['port'])?$info['port']:'3306';
            $this->dbname = isset($info['dbname '])?$info['dbname ']:'php0810';
            $this->charset = isset($info['charset '])?$info['charset ']:'utf8';
            $this->user = isset($info['user'])?$info['user']:'root';
            $this->pass = isset($info['pass'])?$info['pass']:'1234';
            
            //自动连接:当类实例化时自动连接
            $this->db_connect();
                        
        }
        //数据库连接方法
        public function db_connect(){
            $dsn = "{$this->type}:{$this->host};port={$this->port};charset={$this->charset};dbname={$this->dbname}";

            try{
            //连接认证
            $this->pdo = new PDO($dsn,$this->user,$this->pass);
            //修改PDO::FETCH()属性,获取数据为关联数据
            $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
            }catch(PDOException $e){
                echo "链接失败!".'<br/>';
                echo "错误代码:".$e->getCode().'<br/>';
                echo "错误文件:".$e->getFile().'<br/>';
                echo "错误行:".$e->getLine().'<br/>';
                echo "错误信息:".$e->getMessage().'<br/>';
                exit;
            }
        }
        
        //增删改操作:返回影响行或者自增长ID
        public function db_exec($sql){
            $res = $this->pdo->exec($sql);
            //判断
            $this->db_error($res);
        }
        //查找操作:查找一行数据
        public function db_getOne($sql){
            $stmt = $this->pdo->query($sql);
            $this->db_error($stmt);
            return $res = $stmt->fetch();
        }
        //查找很多数据
        public function db_getAll($sql){
            $stmt = $this->pdo->query($sql);
            $this->db_error($stmt);
            return $res = $stmt->fetchAll();
        }
        //验证方法
        public function db_error($sql){
                if($sql ===false){
                echo "SQL语法错误!".'<br/>';
                echo "错误编码:".$this->pdo->errorCode().'<br/>';
                echo "错误信息:".$this->pdo->errorInfo()[2].'<br/>';
                exit;
            }
            return $sql;
        }
        //预处理操作:能够实现,
        public function db_prepare($sql,$args){
            //判断此$sql是否已经产生过预处理
            //执行SQL,完成预处理
            if(!in_array($sql,$this->args)){
                $this->args[$sql] = $sql;
                $stmt = $this->pdo->prepare($sql); //执行与操作
            }
            //执行execute操作 
            $stmt ->execute($args);
            $row = $stmt->fetch();
            return $row;
        }        
    }
    //测试
    $m = new myPDO();
    //var_dump($m);
    //$res = $m->db_exec("update class set classname='PHP0120' where id=1");
    //var_dump($res);
    //$res = $m->db_getOne("select * from class where id=1");
    //$res = $m->db_getAll("select * from my_goods");

    $res = $m->db_prepare("select * from class where id=?",array(2));
    var_dump($res);


转载于:https://my.oschina.net/u/2483876/blog/519290

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值