system.class.inc.php//判断连接的数据库类型以及对数据表的一些操作
system.class.inc.php
1 <?php 2 class ConnDB 3 { 4 var $dbtype; 5 var $host; 6 var $user; 7 var $pwd; 8 var $dbname; 9 var $debug; 10 var $conn; 11 12 function ConnDB($dbtype,$host,$user,$pwd,$dbname,$debug=false){ 13 //构造方法,为成员变量赋值 14 $this->dbtype = $dbtype; 15 $this->host = $host; 16 $this->user = $user; 17 $this->pwd = $pwd; 18 $this->dbname = $dbname; 19 $this->debug=$debug; 20 } 21 22 function GetConnId(){//实现与不同数据库文件的连接并返回连接对象 23 require ("../adodb/adodb.inc.php");//调用ADODB类库文件 24 if($this->dbtype=="mysql" || $this->dbtype=="mssql"){//判断成员变量传递的数据库类型 25 if($this->dbtype=="mysql")//判断如果是MySql数据库 26 $this->conn = NewADOConnection ("mysql");//执行与MySql数据库的连接 27 else 28 $this->conn= NewADOConnection ("mssql"); 29 //数据库连接的用户名与密码 30 $this->conn->Connect($this->host, $this->user, $this->pwd, $this->dbname); 31 } 32 $this->conn->Execute("set names gb2312"); 33 if($this->dbtype=="mysql") 34 $this->conn->debug= $this->debug; 35 return $this->conn; 36 } 37 38 function CloseConnId() 39 { 40 $this->conn->Disconnect(); 41 } 42 43 } 44 class AdminDB{ 45 function ExecSQL($sqlstr,$conn)//定义方法,参数为sql语句和连接数据库返回的对象 46 { 47 $sqltype = strtolower(substr(trim($sqlstr),0,6));//截取sql中的前6个字符串,并转换为小写 48 $rs = $conn->Execute($sqlstr);//执行sql语句 49 if($sqltype=="select"){//判断如果sql语句的类型为select 50 $array = $rs->GetRows(); 51 if(count($array)==0 ||$rs==false)//如果查询结果为0,或者执行失败,返回false 52 return false; 53 else 54 return $array; //否则返回查询结果的数组 55 } 56 else if($sqltype=="update" || $sqltype=="insert" || $sqltype=="delete"){ 57 //判断如果sql语句类型不为select,则执行以下操作 58 if($rs) 59 return true; //成功执行true 60 else 61 return false;//否则false 62 } 63 } 64 } 65 ?>
system.inc.php
数据库连接
1 <?php 2 require("system.class.inc.php"); 3 //数据库连接类实例化 4 $connobj=new ConnDB("mysql","localhost","root","88888888","wordpress",false); 5 $conn=$connobj->GetConnId(); 6 //数据库操作类实例化 7 $admindb=new AdminDB(); 8 ?>
test.php
HTML文本
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 5 <title> ADODB连接、操作数据库类</title> 6 <style type="text/css"> 7 <!-- 8 body,td,th { 9 font-size: 12px; 10 } 11 body { 12 margin-left: 10px; 13 margin-top: 10px; 14 margin-right: 10px; 15 margin-bottom: 10px; 16 } 17 --> 18 </style></head> 19 <body> 20 <table width="644" height="79" border="0"> 21 <tr> 22 <td width="103" align="center">ID</td> 23 <td width="139" align="center">user_login</td> 24 <td width="388" align="center">user_email</td> 25 </tr> 26 <?php 27 include_once 'system.inc.php'; 28 $sqlstr = 'select * from wp_users'; 29 $array = $admindb->ExecSQL($sqlstr,$conn); 30 for($i=0;$i<count($array);$i++){ 31 ?> 32 <tr> 33 <td align="center"><?php echo $array[$i][0];?></td> 34 <td align="center"><?php echo $array[$i][1];?></td> 35 <td align="center"><?php echo $array[$i][4];?></td> 36 </tr> 37 <?php 38 } 39 ?> 40 </table> 41 </body> 42 </html>