space line
space line
space line
space line
space line
您现在的位置是: 功能模块>>

php数据库连接类

发布日期 2006-11-7 5:35:42
作者 admin
出处 整理
浏览次数 ...
演示地址 暂无
授权网站下载 暂无
本地下载
下载地址 我要留言
space line
程序介绍
重用网提供数据库连接类,含mysql、mssql。
space line
使用说明

php+MySQL的web服务模式越来越受企业的欢迎,不只是由于它们的开源以及免费,也因为它们的强大的功能。

特此分享下支持MySQL和MSSQL的php连接类,含测试代码,类代码如下:

<?php
/*-------------------------------------------
class Name:get connection for Database
version:1.0.1
author:shemily
created:2006-11-04
tips:Connection for MySQL,MSSQL
alter:fix testConn´s can´t work bug @2008.08.21
--------------------------------------------*/
class Conn{
 var $hostStr;
 var $userName;
 var $userPass;
 var $link;
 
 function Conn($userPass="root",$userName="root",$hostStr="Localhost"){ 
// constructor
  $this->hostStr=$hostStr;
  $this->userName=$userName;
  $this->userPass=$userPass;
 }
 function getConn($dataBase="mysql"){  
//get database link,default for mysql
  if($dataBase=="mysql"){  //mysql
   $link=mysql_connect($this->hostStr, $this->userName, $this->userPass)
       or die("Could not connect: " . mysql_error() . "<br/>");
   $this->link=$link;
   return $link; 
   mysql_close($link);
  }
  elseif($dataBase=="mssql"){ //mssql
   $link=mssql_connect($this->hostStr, $this->userName, $this->userPass)
       or die("Could not connect mssql server!<br/>");
   $this->link=$link;
   return $link; 
   mssql_close($link);     
  }
  else{
   return NULL;
  }
 }
 function setHostStr($hostStr = "Localhost"){
  $this->hostStr=$hostStr;
 }
 
 function getHostStr(){
  return $this->hostStr;
 }
 
 function setUserName($userName = "root"){
  $this->userName=$userName;
 }
 
 function getUserName(){
  return $this->userName;
 }
 
 function setUserPass($userPass = "root"){
  $this->userPass=$userPass;
 }
 
 function getUserPass(){
  return $this->userPass;
 }
 function testConn($data,$sql,$database="mysql"){  //test connection
        $this->getConn($database);
  $link=$this->link;
  if($database=="mysql"){
   mysql_select_db($data, $link) or die ("Can´t use Database : "
. mysql_error() . "<br/>");
   $result=mysql_query($sql) or die("Invalid query : ". mysql_error() . "<br/>");
   while ($row = mysql_fetch_row($result)) {
    printf ("%s: %s  %s: %s<br/>",mysql_field_name($result,0),$row[0],
mysql_field_name($result,1),$row[1]);
   }
   mysql_free_result($result);
  }
  elseif($database=="mssql"){
   mssql_select_db($data, $link) or die ("Can\´t use Database!<br/>");
   $result=mssql_query($sql) or die("Invalid query!<br/>");
   while ($row = mssql_fetch_row($result)) {
    printf ("%s: %s  %s: %s<br/>",mssql_field_name($result,0),$row[0],
mssql_field_name($result,1),$row[1]);
   }
   mssql_free_result($result);
  }
 }
 
 function echoConn(){  //print class name and functions
  echo "Class Name:",get_class($this),"<br/>";
  echo "functions:<br/>";  
  $class_methods = get_class_methods(get_class($this));
  foreach ($class_methods as $method_name) {
      echo $method_name,",";
  }
  echo "<br/>";
  echo "hostStr:",$this->hostStr,"<br/>"; 
  echo "userName:",$this->userName,"<br/>"; 
  echo "userPass:",$this->userPass,"<br/>"; 
 }
}

/*--Test Codes--*/
/*
$testConn=new Conn("");
// or
//$testConn=new Conn();
//$testConn->setHostStr("Localhost");
// or
//$testConn->setHostStr();
//$testConn->setUserName("root");
//$testConn->setUserPass("");
$testConn->echoConn();
echo "<br/><br/>function test:<br/>";
echo "UserName:",$testConn->getUserName(),"<br/>";
echo "UserPass:",$testConn->getUserPass(),"<br/>";
//connection test
$database="mysql";
$data="test";
$sql="select * from ipdata";
//$testConn->testConn($data,$sql,$database);
// or
$testConn->testConn($data,$sql);
*/
/*--Test Codes--*/
?>

 

space line
space line
space line
space line
space line
space line
space line