搜索

Db_mssql_class

Jun 21, 2016 am 09:06 AM
functiongtnbspreturnthis

详细介绍:
   由于工作的原因,需要对SQL SERVER数据库进行操作,根据以前使用的MySQL数据库操作类改写成现在这个对SQL SERVER进行操作的PHP类,可以执行连接数据库,执行SQL语句,查询数据,获得最后一次插入操作的ID号等功能!

/****************************************************************************
            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($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);
 }
}

?>

 



声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能