php MYSQL data operation class_PHP tutorial

WBOY
Release: 2016-07-14 10:10:19
Original
1091 people have browsed it

mysql database operation class, share with everyone


[php]

/**
 *db.class.php create databse object
 *
 *@author Dick 417@668x.net
 *@copyright http://blog.csdn.net/haibrother
 *
 **/

class Dick_Db{
Public $db_host = ''; //Host address
Public $db_username = ''; //Database account
Public $db_password = ''; //Database password
Public $db_name = ''; //Database name
Public $link = ''; //Database connection object
Public $debug = 1; //Whether to enable debugging, the default is enabled
Public $pconnect = 0; //Whether to enable long connections, the default is closed
Public $log_file = 'log/';//Log file directory

/**
*Initialization information
*@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                                                                                    } 
$this->connect();
}  
       
/**
* Get link
**/
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; 
     } 
 
     /**
*Get the last ID of insert
*@param string $sql
**/ 
     public function insert_id(){ 
        return mysql_insert_id($this->link); 
     } 
 
     /**
*Release database object resources
**/ 
     public function free_result($query){ 
        return mysql_free_result($query); 
     } 
 
 
     /**
*Close database connection
**/ 
     public function close(){ 
        if($this->pconnect === 0) return mysql_close($this->link); 
     } 
      
 
 
    /**
* * Integers are processed safely, only integers greater than or equal to 0 are returned
*@param int
*@return int
**/ 
    public function numeric(& $variint){ 
        if (!isset ($variint)) 
            return 0; 
        if (!is_numeric($variint)) 
            return 0; 
 
        //首字符0处理  
        $str_len = strlen($variint); 
        for ($i = 0; $i < $str_len; $i++) { 
            if ($variint[$i] != '0') 
                break; 
        } 
        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)) { 
                return 0; 
            } else { 
                $variint = substr($variint, 0, 10); 
                //兼容MYSQL中INT无符号最大值4294967295  
                $variint = ($variint > 4294967295) ? 4294967295 : $variint; 
                return $variint; 
            } 
        } else { 
            return 0; 
        } 
    } 
 
    /**
*Return mysql error
**/ 
    public function error() { 
        return (($this->link) ? mysql_error($this->link) : mysql_error()); 
    } 
 
 
    /**
*Return mysql errno
**/ 
    public function errno() { 
        return intval(($this->link) ? mysql_errno($this->link) : mysql_errno()); 
    } 
 
    /**
*Write to SQL error log
*@param string
*@param string type
**/ 
    public function write_log($message='',$type='date'){ 
        if(empty($message))return false; 
        if(!in_array($type, array('date','month')))return false; 
        if(!is_dir($this->log_file)){ 
            mkdir($this->log_file); 
        } 
        switch ($type) { 
            case 'month': 
                $file = $this->log_file.date('Y-m').'.log'; 
                break; 
             
            default: 
                $file = $this->log_file.date('Y-m-d').'.log'; 
                break; 
        } 
        if(!file_exists($file)){ 
            file_put_contents($file,''); 
        } 
        if(!is_readable($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"); 
         
    } 
 
 
    /**
* Terminate program
*@param str $message
*@return print
**/ 
     public function halt($message='',$sql=''){ 
        if($this->debug===1){ 
            $error_get_last = error_get_last(); 
            if($message) { 
                $errmsg = "System info: $messagenn"; 
            } 
            $errmsg .= "Time: ".date('Y-m-d H:i:s')."n"; 
            $errmsg .= "Script: ".$error_get_last['file']."nn"; 
            if($sql) { 
                $errmsg .= "SQL: ".htmlspecialchars($sql)."n"; 
            } 
            $errmsg .= "Error:  ".$this->error()."n"; 
            $errmsg .= "Errno.:  ".$this->errno(); 
 
            echo "n"; 
            echo "

"; 
            echo nl2br($errmsg); 
            exit(); 
        } 
         
     } 
 
     /**
*Program test printing
*@param string
*@return print
**/ 
     public function prf($param){ 
        echo '

'; <br>
        print_r($param); <br>
        echo '
'; 
        exit; 
     } 
 
 

################################################# 
####测试程序 
#### 
################################################## 
error_reporting(E_ALL); 
header('Content-type:text/html;charset=utf-8'); 
date_default_timezone_set('Asia/Shanghai'); 
$config = array( 
    'host'     => '192.168.2.1', 
    'username' => 'root', 
    'password' => '', 
    'dbname'   => 'test', 
    ); 
$db = new Dick_Db((object)$config); 
$db     ->pconnect = 1; 
$sql = 'SELECT * FROM t1'; 
$db->prf($db->dickFetch($sql)); 
 
?> 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477537.htmlTechArticlemysql database operation class, share with everyone [php] ?php /***db.class.php create databse object * *@author Dick 417@668x.net *@copyright http://blog.csdn.net/haibrother * **/ class D. ..
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!