首先定义一个数据库连接函数,支持多种不同的数据库,传入一个
SQL语句,将执行结果返回,代码如下:
public function DBConnect($DBType,$DBServer,$DBUser,$DBPass,$DBName,$sqlStr)
{
$StrCon;
$result;
$conn;
$output="";
switch($DBType)
{
Case 0: //Sql Server
//$StrCon ="Driver={SQL Server};Server=$DBServer;Database=$DBName;Uid=$DBUser;Pwd=$DBPass;";
//创建SQL Server数据库连接
$conn= &NewADOConnection('odbc_mssql');
$conn->Connect("Driver={SQL Server};Server=".$DBServer.";Database=".$DBName.";Uid=".$DBuser.";Pwd=".$DBPass.";");
break;
Case 1: //Oracle
//$StrCon ="Provider=MSDAORA.1;Data Source=$DBServer;Password=$DBPass;User ID=$DBUser;Persist Security Info=True;";
//创建 oracle数据库连接
$conn = &NewADOConnection('oci8');
$conn->Connect($DBServer,$DBUser,$DBPass);
break;
Case 2: //Access
//$StrCon ="Driver={Microsoft Access Driver (*.mdb)};Dbq=$DBName;Uid=$DBUser;Pwd=$DBPass;";
//创建Access数据库连接
$conn = &NewADOConnection('access');
$conn->Connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=".$DBName.";Uid=".$DBUser.";Pwd=".$DBPass.";");
break;
Case 3: //mysql
//创建mysql数据库连接
$conn = NewADOConnection('mysql');
$conn->Connect($DBServer,$DBUser,$DBPass,$DBName);
//$conn = &NewADOConnection("mysql://".$DBUser.":".$DBPass."@".$DBServer."/".$DBName);
break;
default:
echo "抱歉,暂不支持此数据库,请检查配置的数据库类型!";
die();
break;
}
public function DBConnect($DBType,$DBServer,$DBUser,$DBPass,$DBName,$sqlStr)
{
$StrCon;
$result;
$conn;
$output="";
switch($DBType)
{
Case 0: //Sql Server
//$StrCon ="Driver={SQL Server};Server=$DBServer;Database=$DBName;Uid=$DBUser;Pwd=$DBPass;";
//创建SQL Server数据库连接
$conn= &NewADOConnection('odbc_mssql');
$conn->Connect("Driver={SQL Server};Server=".$DBServer.";Database=".$DBName.";Uid=".$DBuser.";Pwd=".$DBPass.";");
break;
Case 1: //Oracle
//$StrCon ="Provider=MSDAORA.1;Data Source=$DBServer;Password=$DBPass;User ID=$DBUser;Persist Security Info=True;";
//创建 oracle数据库连接
$conn = &NewADOConnection('oci8');
$conn->Connect($DBServer,$DBUser,$DBPass);
break;
Case 2: //Access
//$StrCon ="Driver={Microsoft Access Driver (*.mdb)};Dbq=$DBName;Uid=$DBUser;Pwd=$DBPass;";
//创建Access数据库连接
$conn = &NewADOConnection('access');
$conn->Connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=".$DBName.";Uid=".$DBUser.";Pwd=".$DBPass.";");
break;
Case 3: //mysql
//创建mysql数据库连接
$conn = NewADOConnection('mysql');
$conn->Connect($DBServer,$DBUser,$DBPass,$DBName);
//$conn = &NewADOConnection("mysql://".$DBUser.":".$DBPass."@".$DBServer."/".$DBName);
break;
default:
echo "抱歉,暂不支持此数据库,请检查配置的数据库类型!";
die();
break;
}
//执行查询操作
$result=$conn->Execute($sqlStr);
//数据库查询失败处理
if ($result==false)
{
die("查询失败");
}
//输出查询结果
while(!$result->EOF)
{
$max=$result->fieldcount();
//$output=array();
for($i=0;$i<$max;$i++)
{
//echo $result->fields[$i].' ';
$res[$i]=$result->fields[$i].' ';
$output=$output.$res[$i];
}
$result->MoveNext();
}
$output=trim($output);
//echo $output;
//关闭数据库连接
$conn->Close();
return $output;
}
$result=$conn->Execute($sqlStr);
//数据库查询失败处理
if ($result==false)
{
die("查询失败");
}
//输出查询结果
while(!$result->EOF)
{
$max=$result->fieldcount();
//$output=array();
for($i=0;$i<$max;$i++)
{
//echo $result->fields[$i].' ';
$res[$i]=$result->fields[$i].' ';
$output=$output.$res[$i];
}
$result->MoveNext();
}
$output=trim($output);
//echo $output;
//关闭数据库连接
$conn->Close();
return $output;
}
定义一个函数,指明需要使用的数据库类型,数据库用户名和密码等,传入SQL语句,调用上面定义的数据库连接方法,执行SQL语句。
public function ExecuteSql($sqlStr)
{
$DBType=1;
$DBServer="arron";
//数据库用户名,不要使用sys这个超级用户
$DBUser="system";
$DBPass="abcde";
$DBName="";
//$sqlStr="SELECT * FROM func;";
$myExpectStr=$this->DBConnect($DBType,$DBServer,$DBUser,$DBPass,$DBName,$sqlStr);
return $myExpectStr;
}
}