【PHP】MySQL 数据库操作类

  1. <?php  
  2. /* 
  3.  * Mysql 数据库操作类 
  4.  *  
  5.  * Author: DYmyw 
  6.  * Email: dymayongwei@163.com 
  7.  * Date: 2012/03/15 
  8.  * Version: 1.0 
  9.  */  
  10. class Mysql{  
  11.     private $host;                  // 数据库主机名  
  12.     private $user;                  // 数据库用户名  
  13.     private $password;              // 数据库用户密码  
  14.     private $database;              // 数据库名  
  15.     private $pconnect;              // 永久连接标示符  
  16.     private $conn;                  // 数据库连接标示符  
  17.     private $result;                // 执行 SQL 语句获取的结果资源集  
  18.     private $result_arr = array();  // 资源集数组  
  19.     public  $debug = 1;             // 调试模式 1:显示错误信息  
  20.       
  21.     // 构造函数,连接数据库  
  22.     public function __construct( $host = ''$user = ''$password = ''$database = ''$pconnect = false ){  
  23.         if ( $host != '' )          $this->host = $host;  
  24.         if ( $user != '' )          $this->user = $user;  
  25.         if ( $password != '' )      $this->password = $password;  
  26.         if ( $database != '' )      $this->database = $database;  
  27.         if ( isset( $pconnect ) )   $this->pconnect = $pconnect;  
  28.           
  29.         if ( !$this->conn ){  
  30.             if ( $this->pconnect ){  
  31.                 $this->conn = @mysql_pconnect( $this->host, $this->user, $this->password );  
  32.             }else {  
  33.                 $this->conn = @mysql_connect( $this->host, $this->user, $this->password );  
  34.             }             
  35.             if ( !$this->conn ){  
  36.                 $this->db_error( '连接数据库服务器失败!''不能连接到指定的数据库服务器,请稍后再试。' );  
  37.             }  
  38.         }  
  39.           
  40.         if ( !$this->select_db( $this->database ) ){  
  41.             $this->db_error( '打开数据库失败!''不能打开指定的数据库,请检查数据库名后再试。' );  
  42.         }  
  43.           
  44.         return $this->conn;  
  45.     }  
  46.       
  47.     // 选择数据库  
  48.     public function select_db( $database ){  
  49.         return @mysql_select_db( $database$this->conn );  
  50.     }  
  51.       
  52.     // 设置字符集  
  53.     public function set_charset( $charset ){  
  54.         return $this->query( "set names '$charset'" );  
  55.     }  
  56.       
  57.     // 执行 SQL 语句  
  58.     public function query( $sql ){  
  59.         if ( $sql == "" )       $this->db_error( 'SQL语句错误!''SQL语句不能为空!' );  
  60.           
  61.         $this->result = @mysql_query( $sql$this->conn );  
  62.           
  63.         if ( !$this->result )    $this->db_error( '执行SQL语句失败!''错误的SQL语句:'$sql );  
  64.           
  65.         array_push$this->result_arr , $this->result );  
  66.         return $this->result;  
  67.     }  
  68.       
  69.     // 获取数据集记录数  
  70.     public function num_rows( $result ){  
  71.         return @mysql_num_rows( $result );  
  72.     }  
  73.       
  74.     // 以关联数组的形式获取数据集  
  75.     public function fetch_array( $result ){  
  76.         return @mysql_fetch_array( $result );  
  77.     }  
  78.       
  79.     // 获取上次 INSERT 操作后产生的 ID  
  80.     public function insert_id(){  
  81.         return @mysql_insert_id( $this->conn );  
  82.     }  
  83.       
  84.     // 析构函数,自动关闭数据库,垃圾回收  
  85.     public function __destruct(){  
  86.         if ( $this->result_arr && is_array$this->result_arr ) ){  
  87.             foreach ( $this->result_arr as $key => $val ){  
  88.                 @mysql_free_result( $val );  
  89.             }  
  90.         }  
  91.           
  92.         if ( $this->conn )       @mysql_close( $this->conn );  
  93.     }  
  94.       
  95.     // 错误信息  
  96.     public function db_error( $error$detail ){  
  97.         if ( $this->debug == 1 ){  
  98.             $err_msg = "";  
  99.             $err_msg .= "出现数据库错误:"$error ."\n";  
  100.             $err_msg .= "数据库错误信息:"$detail ."\n";  
  101.             $err_msg .= "MySQL错误提示:". mysql_error() ."\n";  
  102.             $err_msg .= "MySQL错误代码:". mysql_errno() ."\n";  
  103.             $err_msg .= "日期:".date("l jS F Y H:i:s A")."\n";  
  104.             $err_msg .= "脚步:"$_SERVER['REQUEST_URI'] ."\n";  
  105.           
  106.             die"<!-- 错误信息:\n"$err_msg ."-->" );  
  107.         }else {  
  108.             die();  
  109.         }  
  110.           
  111.     }  
  112. }  
  113. ?> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值