PHP连接MSSQL Server的类
发布日期:2021-10-21 03:37:53
浏览次数:24
分类:技术文章
本文共 4708 字,大约阅读时间需要 15 分钟。
/*****************************************************************
大白菜芯收藏类之 PHP连接MSSQL Server类由于工作的原因,需要对SQL SERVER数据库进行操作,根据以前使用的MySQL数据库操作类改写成现在这个对SQL SERVER进行操作的PHP类,可以执行连接数据库,执行SQL语句,查询数据,获得最后一次插入操作的ID号等功能!
******************************************************************/
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()){ $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($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){ 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=""){ 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); $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); }}?>
转载地址:https://blog.csdn.net/WapWeb/article/details/22989 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月05日 01时44分31秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
每日一道python面试题 - Python的实例,类和静态方法揭秘
2019-04-28
原创专辑来了
2019-04-28
好好做好你喜欢做的事情,并且把它做好
2019-04-28
反馈不足
2019-04-28
人生永远没有太晚的开始
2019-04-28
python 周日福利来了
2019-04-28
状态模式
2019-04-28
跳表SkipList
2019-04-28
跳跃表(Skip list)原理与java实现
2019-04-28
Java 常见的 30 个误区与细节
2019-04-28
干货|基于 Spring Cloud 的微服务落地
2019-04-28
WEB攻击手段及防御第2篇-SQL注入
2019-04-28
WEB攻击手段及防御第3篇-CSRF
2019-04-28
WEB攻击手段及防御-扩展篇
2019-04-28
spring bean初始化及销毁你必须要掌握的回调方法。
2019-04-28
mysql语句性能开销检测profiling详解
2019-04-28
hashCode到底有什么用?
2019-04-28
设计模式之动态代理模式实战
2019-04-28
设计模式之静态代理模式实战
2019-04-28
作为架构师,你必需要搞清楚的概念:POJO、PO、DTO、DAO、BO、VO
2019-04-28