简单的数据库连接类,适合于多种数据库连接:
<?php
//数据库连接类
class Conn{
$dbtype;
$host;
$user;
$pwd;
$dbname;
$debug;
$conn;
function Conn($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;
}
}
class ConnDB{
var $dbtype;
var $host;
var $user;
var $pwd;
var $dbname;
var $debug;
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(){ //实现与不同数据库的连接并返回连接对象
require("../adodb5/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 utf8"); //设置数据库的编码格式
if($this->dbtype=="mysql")
$this->conn->debug=$this->debug;
return $this->conn; //返回连接对象
}
function CloseConnId(){ //定义关闭数据库的方法
$this->conn->Disconnect(); //执行关闭的操作
}
}
?>
---------------------------------------
只需要在conn.php页面如此调用
<?php
require("found.database.php"); //包含类文件
//数据库连接类实例化
$connobj=new ConnDB("mysql","localhost","root","111","db_database13",false);
$conn=$connobj->GetConnId(); //返回连接标识
?>
------------------