PHP的MSSql的操作类

PHP代码
  1. <?php   
  2. /*MSSql的操作类*/  
  3. class MSSql {   
  4.     var $link;   
  5.     var $querynum = 0;   
  6.   
  7.     /*连接MSSql数据库,参数:dbsn->数据库服务器地址,dbun->登陆用户名,dbpw->登陆密码,dbname->数据库名字*/  
  8.     function Connect($dbsn$dbun$dbpw$dbname) {   
  9.         if($this->link = @mssql_connect($dbsn$dbun$dbpw, true)) {   
  10.             $query = $this->Query('SET TEXTSIZE 2147483647');   
  11.             if (@mssql_select_db($dbname$this->link)) {   
  12.             } else {   
  13.                 $this->halt('Can not Select DataBase');   
  14.             }   
  15.         } else {   
  16.             $this->halt('Can not connect to MSSQL server');   
  17.         }   
  18.     }   
  19.   
  20.     /*执行sql语句,返回对应的结果标识*/  
  21.     function Query($sql) {   
  22.         if($query = @mssql_query($sql$this->link)) {   
  23.             $this->querynum++;   
  24.             return $query;   
  25.         } else {   
  26.             $this->querynum++;   
  27.             $this->halt('MSSQL Query Error'$sql);   
  28.         }   
  29.     }   
  30.   
  31.     /*执行Insert Into语句,并返回最后的insert操作所产生的自动增长的id*/  
  32.     function Insert($table$iarr) {   
  33.         $value = $this->InsertSql($iarr);   
  34.         $query = $this->Query('INSERT INTO ' . $table . ' ' . $value . '; SELECT SCOPE_IDENTITY() AS [insertid];');   
  35.         $record = $this->GetRow($query);   
  36.         $this->Clear($query);   
  37.         return $record['insertid'];   
  38.     }   
  39.   
  40.     /*执行Update语句,并返回最后的update操作所影响的行数*/  
  41.     function Update($table$uarr$condition = '') {   
  42.         $value = $this->UpdateSql($uarr);   
  43.         if ($condition) {   
  44.             $condition = ' WHERE ' . $condition;   
  45.         }   
  46.         $query = $this->Query('UPDATE ' . $table . ' SET ' . $value . $condition . '; SELECT @@ROWCOUNT AS [rowcount];');   
  47.         $record = $this->GetRow($query);   
  48.         $this->Clear($query);   
  49.         return $record['rowcount'];   
  50.     }   
  51.   
  52.     /*执行Delete语句,并返回最后的Delete操作所影响的行数*/  
  53.     function Delete($table$condition = '') {   
  54.         if ($condition) {   
  55.             $condition = ' WHERE ' . $condition;   
  56.         }   
  57.         $query = $this->Query('DELETE ' . $table . $condition . '; SELECT @@ROWCOUNT AS [rowcount];');   
  58.         $record = $this->GetRow($query);   
  59.         $this->Clear($query);   
  60.         return $record['rowcount'];   
  61.     }   
  62.   
  63.     /*将字符转为可以安全保存的mssql值,比如a'a转为a''a*/  
  64.     function EnCode($str) {   
  65.         return str_replace('''''''str_replace(''''$str));   
  66.     }   
  67.   
  68.     /*将可以安全保存的mssql值转为正常的值,比如a''a转为a'a*/  
  69.     function DeCode($str) {   
  70.         return str_replace('''''''$str);   
  71.     }   
  72.   
  73.     /*将对应的列和值生成对应的insert语句,如:array('id' => 1, 'name' => 'name')返回([id], [name]) VALUES (1, 'name')*/  
  74.     function InsertSql($iarr) {   
  75.         if (is_array($iarr)) {   
  76.             $fstr = '';   
  77.             $vstr = '';   
  78.             foreach ($iarr as $key => $val) {   
  79.                 $fstr .= '[' . $key . '], ';   
  80.                 $vstr .= ''' . $val . '', ';   
  81.             }   
  82.             if ($fstr) {   
  83.                 $fstr = '(' . substr($fstr, 0, -2) . ')';   
  84.                 $vstr = '(' . substr($vstr, 0, -2) . ')';   
  85.                 return $fstr . ' VALUES ' . $vstr;   
  86.             } else {   
  87.                 return '';   
  88.             }   
  89.         } else {   
  90.             return '';   
  91.         }   
  92.     }   
  93.   
  94.     /*将对应的列和值生成对应的insert语句,如:array('id' => 1, 'name' => 'name')返回[id] = 1, [name] = 'name'*/  
  95.     function UpdateSql($uarr) {   
  96.         if (is_array($uarr)) {   
  97.             $ustr = '';   
  98.             foreach ($uarr as $key => $val) {   
  99.                 $ustr .= '[' . $key . '] = '' . $val . '', ';   
  100.             }   
  101.             if ($ustr) {   
  102.                 return substr($ustr, 0, -2);   
  103.             } else {   
  104.                 return '';   
  105.             }   
  106.         } else {   
  107.             return '';   
  108.         }   
  109.     }   
  110.   
  111.     /*返回对应的查询标识的结果的一行*/  
  112.     function GetRow($query$result_type = MSSQL_ASSOC) {   
  113.         return mssql_fetch_array($query$result_type);   
  114.     }   
  115.   
  116.     /*清空查询结果所占用的内存资源*/  
  117.     function Clear($query) {   
  118.         return mssql_free_result($query);   
  119.     }   
  120.   
  121.     /*关闭数据库*/  
  122.     function Close() {   
  123.         return mssql_close($this->link);   
  124.     }   
  125.   
  126.     function halt($message = ''$sql = '') {   
  127.         $message .= '<br />MSSql Error:' . mssql_get_last_message();   
  128.         if ($sql) {   
  129.             $sql = '<br />sql:' . $sql;   
  130.         }   
  131.         exit("DataBase Error.<br />Message:$message $sql");   
  132.     }   
  133. }   
  134. ?>  
引用通告地址: 点击获取引用地址
标签: sql,sqlserver,php
评论: 0 | 引用: 0 | 阅读: 1263 | 打印 | 打包