时间:2019-07-30
概述:ADODB 数据库操作类
PHP ADODB连接、操作数据库类,有了这个类,大家就可以在PHP平台中方便调用数据库,可以是MYSQL、SQLSERVER等数据库:
PHP ADODB连接、操作数据库类ID | 名称 | 版本 | 时间 |
include_once 'conn/connect.database.php';//包含数据库连接和操作类的实例化文件
$sqlstr = 'select * from tb_bccd';//定义SQL语句
$array = $admindb->ExecSQL($sqlstr,$conn);//调用数据库操作类中的方法,执行查询操作
for($i=0;$i
?>
<?php echo $array[$i][0];?><?php echo $array[$i][1];?><?php echo $array[$i][2];?><?php echo $array[$i][3];?>}
?>
所包含的文件connect.database.php代码:
require("found.database.php");//包含类文件
//数据库连接类实例化
$connobj=new ConnDB("mysql","localhost","root","111","db_database13",false);
$conn=$connobj->GetConnId();//返回连接标识
//数据库操作类实例化
$admindb=new AdminDB();
?>
found.database.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("../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();//执行关闭的操作
}
}
//数据库管理类
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
}
}
}
?>