打印

Virtual Class -- cannot instantiate 封装ADODB连接数据库类

[复制链接]
1629|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
z372846985|  楼主 | 2012-2-4 13:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 z372846985 于 2012-2-4 13:48 编辑

封装ADODB连接数据库类 提示Virtual Class -- cannot instantiate
代码
conn.class.php

<?php
class ConnDB{
var $dbtype;        //数据库的类型
var $host;         //服务器名称
var $user;         //用户名
var $pwd;         //密码
var $dbname;        //数据库名称
var $debug;         //是否返回执行的数据库,默认设置为返回SQL
var $conn;         //连接数据库时返回的连接标识
//构造方法,为成员数变量赋值
function ConnDB($dbtype,$host,$user,$pwd,$dbname,$debug=true){
  $this->dbtype=$dbtype;
  $this->host=$host;
  $this->user=$user;
  $this->pwd=$pwd;
  $this->dbname=$dbname;
  $this->debug=$debug;
}
//实现与不同数据库的连接并返回连接对象         
function GetConnId(){
  include_once 'adodb/adodb.inc.php';   //调用数据库类库文件
  if ($this->dbtype=="mysql"||$this->dbtype=="mssql") { //判断变量传递的数据库类型
   if ($this->dbtype=="mysql")    //如果是MYSQL数据库
   $this->conn=new ADOConnection("mysql"); //执行与MYSQL数据库的连接
   else
   $this->conn=new ADOConnection("mssql");
   $this->conn->Connect($this->host,$this->user,$this->pwd,$this->dbname);//数据库连接的地址、用户名、密码、数据库名
  }elseif ($this->dbtype=="access"){
   $this->conn=new ADOConnection("access");
   $this->conn->Connect("Driver={microsoft access driver(*.mdb)};dbq=$this->dbname;uid=$this->user;pwd=$this->pwd;");
  }
  $this->conn->Execute("set names utf8");  //设置数据库的编码格式
  if ($this->dbtype=="mysql")
  $this->conn->debug=$this->debug;
  return $this->conn;
}
//定义关闭数据的方法
function CloseConnId(){
  $this->conn->Disconnect();    //执行关闭数据库的操作
}


}
?>
link_conn.php
<?php
include_once 'conn.class.php';
$connobj=new ConnDB("mysql","localhost","root","xuchen","xuchen",false);
$conn=$connobj->GetConnId();
?>
index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 封装ADODB连接数据库类</title>
</head>
<body>
<?php
include_once 'link_conn.php';   //包含数据库连接类的实例化文件
if($conn){
echo "<script>alert('数据库连接成功!');window.location.href=‘123.php’;</script>";
}else{
echo "<script>alert('数据库连接失败!');window.location.href='index.php';</script>";
}
?>

</body>
</html>

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

20

帖子

0

粉丝