PHP操作MYSQL数据库类

 

<? php
 
require_once ( " sys_info.php " );
 
 
// 文件名:ClassDataBase.php
 //类名:数据库类 ClassDataBase
 //作者:感染源
 //日期:2007-1-4

 
 
class  ClassDataBase
 {
  
// 属性
   private   $host ;      // 数据库主机名
   private   $user ;      // 用户名
   private   $pwd ;      // 密码
  
  
private   $dbname ;     // 数据库名
  
  
private   $ConnId ;     // 数据库链接标识
   private   $db_selected ;    // 数据库选择标识
   private   $result ;     // 数据库查询返回结构集
  
  
  //构造函数,链接数据库

   function  __construct()
  {
   
$this -> host  =  sys_info :: $HOST ;
   
$this -> user  =  sys_info :: $USER ;
   
$this -> pwd  =  sys_info :: $PWD ;
   
   
$this -> dbname  =  sys_info :: $DBNAME ;
   
   
   
// 链接数据库
    $this -> ConnId  =   mysql_connect ( $this -> host , $this -> user , $this -> pwd);
   
   
if ( ! $this -> ConnId)
   {
    
die ( ' Sorry,Connect database false!  ' . mysql_error ());
   }
   
else
   {
    
    
// 选择数据库
     $this -> db_selected  =   mysql_select_db ( $this -> dbname , $this -> ConnId);
    
if ( ! $this -> db_selected)
    {
     
die ( ' Can't select the database: ' . mysql_error ());
    }
// if;
   } // if;
  } // function __construct();
  
  
  //析构函数,关闭数据库

   function  __destruct()
  {
   
// 关闭数据库链接
    mysql_close ( $this -> ConnId);
  }
// function __destruct();
  
  
  //执行数据库的更新,插入和删除

   function  ExecuteSQL( $sql )
  {
   
$this -> result  =   mysql_query ( $sql );
   
   
if ( ! $this -> result)
   {
    
die ( ' Can't update the database:  ' . mysql_error ());
   }
// if
  } // function ExecuteSQL();
  
  
  //执行数据库的查询,并返回查询结果

   function  QuerySQL( $sql )
  {
   
$i   =   0 ;
   
$result_arr   =   array ();
   
   
$this -> result  =   mysql_query ( $sql , $this -> ConnId);
   
   
if ( ! $this -> result)
   {
    
die ( ' Can't view the database:  ' . mysql_error ());
   }
   
else
   {
    
while ( $row   =   mysql_fetch_array ( $this -> result))
    {
     
$result_arr [ $i ++ =   $row ;
    }
// while
    
    
return   $result_arr ;
    
    
// 释放内存
     mysql_free_result ( $this -> result);
   }
// if
  } // function QuerySQL();
 } // ClassDataBase;

?>  



<? php
 
 
// 文件名:sys_info.inc
 //类名:环境变量类 sys_info
 //作者:感染源
 //时间:2007-1-4

 
 
class  sys_info
 {
  
public   static   $HOST   =   " localhost " ;   // MySql Host Name
   public   static   $USER   =   " root " ;    // MySql Login Name
   public   static   $PWD   =   " root " ;    // MySql Login Password
  
  
public   static   $DBNAME   =   " books " ;   // 数据库名
  
  
public   static   $MAX_LINE   =   5 ;    // 每页显示最多行数
  
 }
// class sys_info;
?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值