php教程 PHP开发 PHP로 캡슐화된 MSSql 작업 클래스의 전체 예

PHP로 캡슐화된 MSSql 작업 클래스의 전체 예

Dec 21, 2016 pm 05:09 PM

이 기사의 예에서는 PHP로 캡슐화된 MSSql 작업 클래스를 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

<?php
/*MSSql的操作类*/
class MSSql {
  var $link;
  var $querynum = 0;
  /*连接MSSql数据库,参数:dbsn->数据库服务器地址,dbun->登陆用户名,dbpw->登陆密码,dbname->数据库名字*/
  function Connect($dbsn, $dbun, $dbpw, $dbname) {
    if($this->link = @mssql_connect($dbsn, $dbun, $dbpw, true)) {
      $query = $this->Query(&#39;SET TEXTSIZE 2147483647&#39;);
      if (@mssql_select_db($dbname, $this->link)) {
      } else {
        $this->halt(&#39;Can not Select DataBase&#39;);
      }
    } else {
      $this->halt(&#39;Can not connect to MSSQL server&#39;);
    }
  }
  /*执行sql语句,返回对应的结果标识*/
  function Query($sql) {
    if($query = @mssql_query($sql, $this->link)) {
      $this->querynum++;
      return $query;
    } else {
      $this->querynum++;
      $this->halt(&#39;MSSQL Query Error&#39;, $sql);
    }
  }
  /*执行Insert Into语句,并返回最后的insert操作所产生的自动增长的id*/
  function Insert($table, $iarr) {
    $value = $this->InsertSql($iarr);
    $query = $this->Query(&#39;INSERT INTO &#39; . $table . &#39; &#39; . $value . &#39;; SELECT SCOPE_IDENTITY() AS [insertid];&#39;);
    $record = $this->GetRow($query);
    $this->Clear($query);
    return $record[&#39;insertid&#39;];
  }
  /*执行Update语句,并返回最后的update操作所影响的行数*/
  function Update($table, $uarr, $condition = &#39;&#39;) {
    $value = $this->UpdateSql($uarr);
    if ($condition) {
      $condition = &#39; WHERE &#39; . $condition;
    }
    $query = $this->Query(&#39;UPDATE &#39; . $table . &#39; SET &#39; . $value . $condition . &#39;; SELECT @@ROWCOUNT AS [rowcount];&#39;);
    $record = $this->GetRow($query);
    $this->Clear($query);
    return $record[&#39;rowcount&#39;];
  }
  /*执行Delete语句,并返回最后的Delete操作所影响的行数*/
  function Delete($table, $condition = &#39;&#39;) {
    if ($condition) {
      $condition = &#39; WHERE &#39; . $condition;
    }
    $query = $this->Query(&#39;DELETE &#39; . $table . $condition . &#39;; SELECT @@ROWCOUNT AS [rowcount];&#39;);
    $record = $this->GetRow($query);
    $this->Clear($query);
    return $record[&#39;rowcount&#39;];
  }
  /*将字符转为可以安全保存的mssql值,比如a&#39;a转为a&#39;&#39;a*/
  function EnCode($str) {
    return str_replace(&#39;&#39;&#39;, &#39;&#39;&#39;&#39;, str_replace(&#39;&#39;, &#39;&#39;, $str));
  }
  /*将可以安全保存的mssql值转为正常的值,比如a&#39;&#39;a转为a&#39;a*/
  function DeCode($str) {
    return str_replace(&#39;&#39;&#39;&#39;, &#39;&#39;&#39;, $str);
  }
  /*将对应的列和值生成对应的insert语句,如:array(&#39;id&#39; => 1, &#39;name&#39; => &#39;name&#39;)返回([id], [name]) VALUES (1, &#39;name&#39;)*/
  function InsertSql($iarr) {
    if (is_array($iarr)) {
      $fstr = &#39;&#39;;
      $vstr = &#39;&#39;;
      foreach ($iarr as $key => $val) {
        $fstr .= &#39;[&#39; . $key . &#39;], &#39;;
        $vstr .= &#39;&#39;&#39; . $val . &#39;&#39;, &#39;;
      }
      if ($fstr) {
        $fstr = &#39;(&#39; . substr($fstr, 0, -2) . &#39;)&#39;;
        $vstr = &#39;(&#39; . substr($vstr, 0, -2) . &#39;)&#39;;
        return $fstr . &#39; VALUES &#39; . $vstr;
      } else {
        return &#39;&#39;;
      }
    } else {
      return &#39;&#39;;
    }
  }
  /*将对应的列和值生成对应的insert语句,如:array(&#39;id&#39; => 1, &#39;name&#39; => &#39;name&#39;)返回[id] = 1, [name] = &#39;name&#39;*/
  function UpdateSql($uarr) {
    if (is_array($uarr)) {
      $ustr = &#39;&#39;;
      foreach ($uarr as $key => $val) {
        $ustr .= &#39;[&#39; . $key . &#39;] = &#39;&#39; . $val . &#39;&#39;, &#39;;
      }
      if ($ustr) {
        return substr($ustr, 0, -2);
      } else {
        return &#39;&#39;;
      }
    } else {
      return &#39;&#39;;
    }
  }
  /*返回对应的查询标识的结果的一行*/
  function GetRow($query, $result_type = MSSQL_ASSOC) {
    return mssql_fetch_array($query, $result_type);
  }
  /*清空查询结果所占用的内存资源*/
  function Clear($query) {
    return mssql_free_result($query);
  }
  /*关闭数据库*/
  function Close() {
    return mssql_close($this->link);
  }
  function halt($message = &#39;&#39;, $sql = &#39;&#39;) {
    $message .= &#39;<br />MSSql Error:&#39; . mssql_get_last_message();
    if ($sql) {
      $sql = &#39;<br />sql:&#39; . $sql;
    }
    exit("DataBase Error.<br />Message $message $sql");
  }
}
?>
로그인 후 복사

이 기사가 PHP 프로그래밍에 종사하는 모든 사람에게 도움이 되기를 바랍니다.

PHP로 캡슐화된 MSSql 작업 클래스의 더 완전한 예를 보려면 PHP 중국어 웹사이트를 주목하세요!


본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)