PHP encapsulated MSSQL Operation class complete Instance _php skill

Source: Internet
Author: User

The examples in this article describe the MSSQL operations classes in PHP encapsulation. Share to everyone for your reference, specific as follows:

<?php/*mssql Operation class/class MSSql {var $link;
  var $querynum = 0; /* Connect MSSQL Database, parameter:dbsn-> database server address,dbun-> login username,dbpw-> login Password,dbname-> database name/function connect ($DBSN, $dbun, $ DBPW, $dbname) {if ($this->link = @mssql_connect ($DBSN, $dbun, $DBPW, True)) {$query = $this->query (' SET
      Textsize 2147483647 ');
      if (@mssql_select_db ($dbname, $this->link)) {} else {$this->halt (' Can not select DataBase ');
    } else {$this->halt (' Can not connect to MSSQL server '); }/* Executes the SQL statement, returns the corresponding result identifier/function Query ($sql) {if ($query = @mssql_query ($sql, $this->link)) {$this-&
      gt;querynum++;
    return $query;
      else {$this->querynum++;
    $this->halt (' MSSQL Query Error ', $sql); }/* Executes the INSERT INTO statement and returns the automatic growth of the id*/function insert ($table, $iarr) {$value = $this->insertsql, generated by the last insert operation (
    $iarr); $query = $this->query (' INSERT into '. $table. ' ' . $value. '; SELECTScope_identity () as [Insertid];
    $record = $this->getrow ($query);
    $this->clear ($query);
  return $record [' Insertid ']; /* Executes the UPDATE statement and returns the number of rows affected by the last Update operation/function Update ($table, $uarr, $condition = ') {$value = $this->updates
    QL ($uarr);
    if ($condition) {$condition = ' WHERE '. $condition; $query = $this->query (' UPDATE '. $table. ' SET '. $value. $condition. ';
    SELECT @ @ROWCOUNT as [rowcount];
    $record = $this->getrow ($query);
    $this->clear ($query);
  return $record [' rowcount ']; /* Executes the DELETE statement and returns the number of rows affected by the last delete operation */function Delete ($table, $condition = ') {if ($condition) {$conditio n = ' WHERE '.
    $condition; $query = $this->query (' DELETE '. $table. $condition. ';
    SELECT @ @ROWCOUNT as [rowcount];
    $record = $this->getrow ($query);
    $this->clear ($query);
  return $record [' rowcount ']; }/* Converts a character to a safe-saved MSSQL value, such as a ' A to a ' a*/function EnCode ($str) {RetuRN Str_replace (', ', ', ', Str_replace (', ', $str));
  /* To convert the MSSQL value that can be safely saved to a normal value, such as a ' ' A to a ' a*/function DeCode ($str) {return str_replace (', ', ', $str); */* Generates corresponding INSERT statements for corresponding columns and values, such as: Array (' ID ' => 1, ' name ' => ' name ') returns ([ID], [name]] VALUES (1, ' name ')/function Inse
      Rtsql ($iarr) {if (Is_array ($iarr)) {$fstr = ';
      $vstr = '; foreach ($iarr as $key => $val) {$fstr. = ' ['. $key.
        '], '; $vstr. = "". $val.
      '', '; } if ($fstr) {$fstr = ' ('. substr ($fstr, 0,-2).
        ')'; $vstr = ' ('. substr ($vstr, 0,-2).
        ')'; Return $FSTR. ' VALUES '.
      $VSTR;
      else {return ';
    } else {return '; }/* generates the corresponding INSERT statement for the corresponding column and value, such as: Array (' ID ' => 1, ' name ' => ' name ') returns [id] = 1, [name] = ' name '/function Updatesq
      L ($uarr) {if (Is_array ($uarr)) {$ustr = '; foreach ($uarr as $key => $val) {$ustr. = ' ['. $key. '] = '' . $val. '', ';
      } if ($USTR) {return substr ($ustr, 0,-2);
      else {return ';
    } else {return '; }//* Returns a line/function GetRow ($query, $result _type = MSSQL_ASSOC) of the result of the corresponding query identification {return Mssql_fetch_array ($query, $
  Result_type);
  } * * Empty the memory resource occupied by the query result/function Clear ($query) {return mssql_free_result ($query);
  /* Shut down database/function close () {return mssql_close ($this->link);
    function Halt ($message = ', $sql = ') {$message. = ' <br/>mssql Error: '. Mssql_get_last_message ();
    if ($sql) {$sql = ' <br/>sql: '. $sql;
  Exit ("DataBase error.<br/>message $message $sql");

 }}?>

More about PHP Interested readers can view the site topics: "PHP based on PDO Operation Database Skills Summary", "PHP+MONGODB Database Operation Skills Encyclopedia", "PHP object-oriented Programming Program Introduction", "PHP string (String) Usage Summary", " Introduction to PHP+MYSQL Database operations and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.