PHP 连接 SQL SERVER 类源程序参考

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"; www@bitscn@com

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.");
www_bitscn_com中国.网管联盟

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"; 中国_网管联盟bitsCN.com
$res = mssql_query($sql,$this->LinkID);
$this->LastInsertID = mssql_result($res,0,id);
}
return $this->QueryResult;
}


/**public
* function: Get the query result's row number
* success: return the row fo the Result
* failed: return 0
*/
function getTotalRowNum($result=""){
if($result != "") $this->QueryResult = $result;
$row = @mssql_num_rows($this->QueryResult);
if($row >= 0) return $row;
$this->halt("Get a row of result Failed: Result $result is invalid.");
return 0;
}


/**public
* function: Get the last insert record's id
* success: return id
* failed: return 0
*/
function lastInsertID(){
return $this->LastInsertID;
}


/**public
* function: Get a field's value
* success: return value of the field
* failed: return 0
*/
function getField($result="",$row=0,$field=0){

BBS.bitsCN.com网管论坛


if($result != "") $this->QueryResult = $result;
$fieldvalue = @mssql_result($this->QueryResult,$row,$field);
if($fieldvalue != "") return $fieldvalue;
$this->halt("Get field: mssql_result($this->QueryResult,$row,$field) failed.");
return 0;

//Here should have error handle
}


/**public
* function: Get next record
* success: return a array of the record's value
* failed: return 0
*/
function nextRecord($result=""){
if($result != "") $this->QueryResult = $result;
$record = @mssql_fetch_array($this->QueryResult);
if(is_array($record)) return $record;
//$this->halt("Get the next record Failed: the Result $result is invalid.");
return 0;
}


/**public
* function: Free the Query Result
* success return 1
* failed: return 0
*/
function freeResult($result=""){ bitsCN#com中国网管联盟
if($result != "") $this->QueryResult = $result;
return @mssql_free_result($this->QueryResult);
}


/**public
* function: Set the Halt_On_Error's state
* success: return 1
* failed: return 0
*/
function setHaltOnError($state="ignore"){
if(!($state == "ignore" || $state == "report" || $state == "halt")){
$this->halt("Set the Halt_On_Error Fail: There is no state value $state");
return 0;
}
$this->Halt_On_Error = $state;
return 1;
}


/**public
* function: Get the Halt_On_Error's state
*/
function getHaltOnError(){
return $this->Halt_On_Error;
}


/**public
* function: Get the class's name
*/
function toString(){
return $this->ClassName;
}


/**private
* function: Error handle
*/
function halt($msg){
$this->Error = @mysql_error($this->LinkID); DL@bitsCN_com网管软件下载
$this->ErrNo = @mysql_errno($this->LinkID);
if ($this->Halt_On_Error == "ignore") return;
$this->makeMsg($msg);
if ($this->Halt_On_Error == "halt") die(" Session halted");
}


/**private
* function: Make error information and print
*/
function makeMsg($msg){
printf(" Database error: %s
/n", $msg);
printf(" MySQL Error: %s (%s)
/n",$this->ErrNo,$this->Error);
}
}

?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值