首页  >  文章  >  php教程  >  php MYSQL数据操作类

php MYSQL数据操作类

WBOY
WBOY原创
2016-06-13 10:56:18997浏览

mysql 数据库操作类,分享给大家


[php]

/**
 *db.class.php 创建数据库对象
 *
 *@作者迪克 417@668x.net
 *@copyright http://blog.csdn.net/haibrother
 *
 **/ 
 
class Dick_Db{ 
    public  $db_host        = ''; //主机地址  
    public  $db_username    = ''; //数据库帐号  
    public  $db_password    = ''; //数据库密码  
    public  $db_name        = ''; //数据库名  
    public  $link           = ''; //数据库连接对象  
    public  $debug          = 1; //是否开启debug调试,默认是开启的  
    public  $pconnect       = 0; //是否开启长连接,默认是关闭的  
    public  $log_file       = 'log/';//日志文件目录  
 
    /**
     *初始化信息
     *@param object
     **/ 
    public function __construct($config=''){ 
        if(!is_object($config)){ 
            $this->halt('database config is not wrong,please check it!'); 
        } 
        if(!empty($config)){ 
            $this->db_host     = $config->host; 
            $this->db_username = $config->username; 
            $this->db_password = $config->password; 
            $this->db_name     = $config->dbname;        
        } 
        $this->connect(); 
    } 
     
    /**
     * 获取链接
     **/ 
     public function connect(){ 
         $connect = $this->pconnect === 1?'mysql_pconnect':'mysql_connect'; 
         if(!$this->link = @$connect($this->db_host,$this->db_username,$this->db_password)){ 
            $this->halt('Database cant not connect!'); 
         } 
          mysql_set_charset('utf8',$this->link); 
          mysql_select_db($this->db_name,$this->link); 
     } 
 
 
     /**
      *查询
      *@param 字符串 $sql
      *返回布尔值
      **/ 
     public function query($sql){ 
        if(!$query = mysql_query($sql,$this->link)){ 
             $message = date('Y-m-d H:i:s').'  '.$sql; 
             $this-> write_log($message); 
             $this->halt('SQL error,please check it!',$sql); 
        } 
        return $query; 
     } 
 
     /**
      *
      *@param string $sql
      *@param 字符串 $type
      * mysql_fetch_assoc mysql_fetch_array mysql_fetch_row mysql_affected_rows
      *@return 数组
      */ 
     公共函数 fetch($sql,$type='assoc'){ 
        $fetch_type = 'mysql_fetch_'.$type; 
        $query = $this->query($sql); 
        $结果 = $fetch_type($query); 
        $this->free_result($query); 
        $this->close(); 
        返回$结果; 
     } 
 
     /**
      *@param 字符串 $sql
      *@return 数组
      **/ 
     公共函数 dickFetch($sql,$type='assoc'){ 
        $fetch_type = 'mysql_fetch_'.$type; 
        $query = $this->query($sql); 
        $行=数组(); 
        while ($row=$fetch_type($query)) { 
            $行[]=$行; 
        } 
        $this->free_result($query); 
        $this->close(); 
        返回$行; 
     } 
 
     /**
      *取得insert 最后一次的ID
      *@param string $sql
      **/ 
     公共函数 insert_id(){ 
        返回 mysql_insert_id($this->link); 
     } 
 
     /**
      *释放数据库对象资源
      **/ 
     公共函数 free_result($query){ 
        返回 mysql_free_result($query); 
     } 
 
 
     /**
      *关闭数据库连接
      **/ 
     公共函数 close(){ 
        if($this->pconnect === 0) return mysql_close($this->link); 
     } 
      
 
 
    /**
     * 整数安全处理,仅返回大于等于0的整数
     *@param int
     *@return int
     **/ 
    公共函数 numeric(& $variint){ 
        if (!isset ($variint))
            返回0; 
        if (!is_numeric($variint))
            返回0; 
 
        //首字符0处理  
        $str_len = strlen($variint); 
        for ($i = 0; $i             if ($variint[$i] != '0') 
                休息; 
        } 
        if ($i > 0 && $variint > 0) { 
            $variint = substr($variint, $i, $str_len); 
            $str_len = strlen($variint); 
        } 
 
        //数字安全处理  
        if ($str_len > 0) { 
            if (!preg_match("/^[0-9] $/", $variint)) { 
                返回0; 
            }其他{ 
                $variint = substr($variint, 0, 10); 
                //兼容MYSQL中INT无符号顶部4294967295  
                $variint = ($variint > 4294967295) ? 4294967295 : $variint; 
                返回$variint; 
            } 
        }其他{ 
            返回0; 
        } 
    } 
 
    /**
     *返回mysql error
     **/ 
    公共函数错误(){ 
        return (($this->link) ? mysql_error($this->link) : mysql_error()); 
    } 
 
 
    /**
     *返回mysql errno
     **/ 
    公共函数 errno() { 
        return intval(($this->link) ? mysql_errno($this->link) : mysql_errno()); 
    } 
 
    /**
     *写入SQL错误日志
     *@param string
     *@param string type
     **/ 
    公共函数 write_log($message='',$type='date'){ 
        if(空($message))返回 false; 
        if(!in_array($type, array('日期','月份')))返回 false; 
        if(!is_dir($this->log_file)){ 
            mkdir($this->log_file); 
        } 
        开关($类型){ 
            案例“月份”:
                $file = $this->log_file.date('Y-m').'.log'; 
                休息; 
             
            默认值:
                $file = $this->log_file.date('Y-m-d').'.log'; 
                休息; 
        } 
        if(!file_exists($file)){ 
            file_put_contents($file,''); 
        } 
        if(!is_read($file)){ 
            $this->halt($file.'- 系统无权限读操作!');    
        } 
        if(!is_writable($file)){ 
            $this->halt($file.'- 系统无权限写操作!'); 
        } 
        $contents = file_get_contents($file); 
        $add_message = '  -Error:'.$this->error().'   -Errno:'.$this->errno(); 
        file_put_contents($file, $contents.$message.$add_message."n"); 
         
    } 
 
 
    /**
      *终止程序
      *@param str $message
      *@return print
      **/ 
     公共函数暂停($message='',$sql=''){ 
        if($this->debug===1){ 
            $error_get_last = error_get_last(); 
            if($消息) { 
                $errmsg = "系统信息: $messagenn"; 
            } 
            $errmsg .= "时间: ".date('Y-m-d H:i:s')."n"; 
            $errmsg .= "脚本: ".$error_get_last['file']."nn"; 
            if($sql) { 
                $errmsg .= "SQL: ".htmlspecialchars($sql)."n"; 
            } 
            $errmsg .= "错误:  ".$this->error()."n"; 
            $errmsg .= "Errno.:  ".$this->errno(); 
 
            echo "n"; 
            echo "

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn