当前位置:数据库 > SQLServer >>

php MYSQL数据操作类

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


[php]

<?php 
/**
 *db.class.php create databse object
 *
 *@author Dick 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); 
     } 
 
 
     /**
      *query
      *@param string $sql
      *return boolean 
      **/ 
     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 string $type
      * mysql_fetch_assoc  mysql_fetch_array  mysql_fetch_row  mysql_affected_rows
      *@return array
      */ 
     public function fetch($sql,$type='assoc'){ 
        $fetch_type = 'mysql_fetch_'.$type; 
        $query = $this->query($sql); 
        $result = $fetch_type($query); 
        $this->free_result($query); 
        $this->close(); 
        return $result; 
     } 
 
     /**
      *@param string $sql
      *@return array
      **/ 
     public function dickFetch($sql,$type='assoc'){ 
        $fetch_type = 'mysql_fetch_'.$type; 
        $query = $this->query($sql); 
        $rows=array(); 
        while ($row=$fetch_type($query)) { 
            $rows[]=$row; 
        } 
        $this->free_result($query); 
        $this->close(); 
        return $rows; 
     } 
 
     /**
      *取得insert 最后一次的ID
      *@param string $sql
      **/ 
     public function insert_id(){ 
        return mysql_insert_id($this->link); 
     } 
 
     /**
      *释放数据库对象资源
      **/ 
     public function free_result($query){ 
        return mysql_free_result($query); 
     } 
 
 
     /**
      *关闭数据库连接
      **/ 
     public function close(){ 
        if($this->pconnect === 0) return mysql_close($this->link); 
     } 
   &

补充:Web开发 , php ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,