PHP_mysql_class

<?php
    header("content-type:text/html;charset=utf-8");
    //封装数据库连接类文件
    class Mysql{
        //定义属性
        private $user;
        private $host;
        private $port;
        private $pass;
        private $dbname;
        private $charset;
        //定义连接方法函数
        public function __construct($info = array()){//数组为关联数组
            $this->user = isset($info['user'])?$info['user']:'root';            
            $this->host = isset($info['host'])?$info['host']:'localhost';            
            $this->port = isset($info['port'])?$info['port']:'3306';            
            $this->pass = isset($info['pass'])?$info['pass']:'1234';            
            $this->dbname = isset($info['dbname'])?$info['dbname']:'php0810';            
            $this->charset = isset($info['charset'])?$info['charset']:'utf8';
            //调用数据库相关操作方法函数
            //由于数据库初始化需要一下三个方法配合,因此将那三个方法封装在此方法内。
            $this -> db_connect();
            $this -> db_charset();
            $this -> db_useda();
        }
        //数据库连接方法
        private function db_connect(){
            //数据库连接
            $res = mysql_connect($this->host.":".$this->port,$this->user,$this->pass);
            if(!$res){
                echo '数据库连接失败';
                echo "错误编码:".mysql_errno().'<br/>';
                echo '错误信息'.iconv('GBK'.'UTF8'.mysql_error()).'<br/>';
                exit;
            }
        }    
        //数据库字符集设置方法
        private function db_charset(){
            //执行
            $this->db_error("set names {$this->charset}");
        }
        //指定数据库方法
        private function db_useda(){            
            //执行
            $this->db_error("use {$this->dbname}");
        }
        private function db_error($sql){
            //验证SQL语句是否正确
            $res = mysql_query($sql);
            if(!$res){
                echo "SQL语法错误";
                echo "错误编码:".mysql_errno()."<br/>";
                echo "错误信息:".iconv('GBK','utf-8',mysql_error())."<br/>";
                exit;//强制退出
            }
            return $res;
        }
        //数据库增删改操作:获取操作引起的自增长或者影响行数的值
        public function db_exec($sql){
            //执行
            $this->db_error($sql);
            //获取操作引起的id自增长
            $id = mysql_insert_id();
            //返回自增长或影响的行数的值
            return isset($id)?$id:mysql_affected_rows();
        }

        //单条数据读取
        public function db_getone($sql){
            //执行SQL语句
            $res = $this->db_error($sql);
            return mysql_fetch_assoc($res);

        }

        //全部数据读取
        public function db_getall($sql){    
            //执行SQL语句
            $res = $this->db_error($sql);            
            //遍历数据
            $lists = array();
            while($line = mysql_fetch_assoc($res)){
                $lists[] = $line;
            }
            return $lists;    
        }
    }    

    //实例化
    $b=new Mysql;
    //var_dump($b);
    //获取制定数据表的全部数据
    //var_dump($b->db_getone('select * from class where id=1'));
    //获取增删改查操作引起的自增长或者影响行数
    //var_dump($b->db_exec("insert into class values(null,'PHP0210','A202')"));


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值