日期:2013-04-26 浏览次数:20517 次
详细介绍:
由于工作的原因,需要对SQL SERVER数据库进行操作,根据以前使用的MySQL数据库操作类改写成现在这个对SQL SERVER进行操作的PHP类,可以执行连接数据库,执行SQL语句,查询数据,获得最后一次插入操作的ID号等功能!
<?php
/****************************************************************************
db_mssql_class.php - description
-------------------
begin : 2002 4 2
$Id: db_mssql_class.php,v 1.1 2002/04/03 09:25:33 Simon.Qiu Exp $
/****************************************************************************/
class DB_Handle{
var $ClassName = "DB_Handle";
var $Server;
var $UserName;
var $Password;
var $Database;
var $LinkID=0;
var $QueryResult="";
var $LastInsertID = "";
/* private ignore=>ignore the error and continue, halt=>report the error and halt, report=>report the error and continue */
var $Halt_On_Error = "report";
var $Error = "";
var $ErrNo = 0;
/**public
* remark: This is the db_mysql_class's structure
* function: Set the server,username,password,database variable.
*/
function DB_Handle($server="",$username="",$password="",$database=""){
$this->Server = $server;
$this->UserName = $username;
$this->Password = $password;
$this->Database = $database;
}
/**public
* function: Connect database and select database
* success: retun 1
* failed: return 0
*/
function connect(){
$this->LinkID = @mssql_pconnect($this->Server,$this->UserName,$this->Password);
if(!$this->LinkID){
$this->halt("mssql_pconnect($this->Server,$this->UserName,$this->Password): Failed");
return 0;
}
if(!@mssql_select_db($this->Database)){
$this->halt("mssql_select_db($this->Database) Failed.");
return 0;
}
return 1;
}
/**public
* function: Check the database, if exist then select
* exist: return 1
* not exist: return 0
*/
function selectDatabase(){
if(@mssql_select_db($this->Database))
return 1;
else
return 0;
}
/**public
* function: Execute SQL instruction
* success: return SQL Result.
* failed: return 0;
*/
function execQuery($sql=""){
if($this->LinkID == 0){
$this->halt("Execute SQL Failed: Hava not valid database connect.");
return 0;
}
ob_start();
$this->QueryResult = mssql_query($sql,$this->LinkID);
$error = ob_get_contents();
ob_end_clean();
if($error){
$this->halt("Execute SQL: mssql_query($sql,$this->LinkID) failed.");
return 0;
}
$reg = "#insert into#";
if(preg_match($reg,$sql)){
$sql = "select @@IDENTITY as id";
$res = mssql_query($sq