面向对象封装ADODB类库+Smarty模板

(1)定义ADODB连接操作类文件system.class.inc

<?php
//数据库连接类
class ConnDB{
 var $dbtype;
 var $host;
     var $user;
     var $pwd;
     var $dbname;
     var $debug;
     var $conn;   
    function ConnDB($dbtype,$host,$user,$pwd,$dbname,$debug=false){ //构造方法,为成员变量赋值
  $this->dbtype=$dbtype;
      $this->host=$host;
  $this->user=$user;
  $this->pwd=$pwd;
  $this->dbname=$dbname;
  $this->debug=$debug;
 }
    function GetConnId(){             //实现与不同数据库的连接并返回连接对象
   require("adodb/adodb.inc.php");      //调用ADODB类库文件
      if($this->dbtype=="mysql" || $this->dbtype=="mssql"){  //判断成员变量传递的数据库类型
       if($this->dbtype=="mysql")      //判断如果是MySQL数据库
           $this->conn=NewADOConnection("mysql");  //执行与MySQl数据库的连接
       else
              $this->conn=NewADOConnection("mssql");
           $this->conn->Connect($this->host,$this->user,$this->pwd,$this->dbname); //数据库连接的用户、密码
  }elseif($this->dbtype=="access"){      //判断如果使用的是Access数据库
           $this->conn=NewADOConnection("access");
           $this->conn->Connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=".$this->dbname.";Uid=".$this->user.";Pwd=".$this->pwd.";"); //执行连接Access数据库
      }
      $this->conn->Execute("set names gb2312");    //设置数据库的编码格式
      if($this->dbtype=="mysql")
           $this->conn->debug=$this->debug;
      return $this->conn;        //返回连接对象
    }
 function CloseConnId(){        //定义关闭数据库的方法
      $this->conn->Disconnect();       //执行关闭的操作
    }

//数据库管理类
class AdminDB{
 function ExecSQL($sqlstr,$conn){     //定义方法,参数为SQl语句和连接数据库返回的对象
  $sqltype=strtolower(substr(trim($sqlstr),0,6)); //截取SQL中的前6个字符串,并转换成小写
  $rs=$conn->Execute($sqlstr);     //执行SQL语句
  if($sqltype=="select"){      //判断如果SQL语句的类型为SELECT
   $array=$rs->GetRows();    //执行该语句,获取查询结果
   if(count($array)==0 || $rs==false)   //判断语句是否执行成功
    return false;     //如果查询结果为0,或者执行失败,则返回false
   else
    return $array;     //否则返回查询结果的数组
  }elseif ($sqltype=="update" || $sqltype=="insert" || $sqltype=="delete"){
   //判断如果SQL语句类型不为select、则执行如下操作
   if($rs)
       return true;      //执行成功返回true
   else
       return false;         //是否返回false
  }
 }
}

?>

 

(2)定义Smarty模板配置类system.smarty.inc。

<?php
require("smarty/smarty/libs/Smarty.class.php");

class SmartyProject extends  Smarty{  //定义类,继承Smarty父类
 function SmartyProject(){   //定义方法,配置Smarty模板
  $this->template_dir = "./";  //指定模板文件存储在根目录下
  $this->compile_dir = "./system/Smarty/templates_c/"; //指定编译文件存储在system/Smarty/templates_c/文件夹下
  $this->config_dir = "./system/Smarty/configs/";
  $this->cache_dir = "./system/Smarty/cache/";
    }

}

?>

 

(3)类的实例化文件system.inc

<?php
require("system.smarty.inc.php");
require("system.class.inc.php");
//数据库连接类实例化
$connobj=new ConnDB("mysql","localhost","root","root","db_database09",false);
$conn=$connobj->GetConnId();
//数据库操作类实例化
$admindb=new AdminDB();

$smarty=new SmartyProject();

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值