The MSSQL operation class in PHP package and a complete example analysis

Source: Internet
Author: User
Tags mssql mssql server rowcount
This article mainly introduces the PHP package of the MSSQL operation class, with a full instance of the PHP package to analyze the various commonly used MSSQL database operations, including the MSSQL database connection and increase the deletion of the search, the need for friends can refer to the next

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 TE      Xtsize 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 and returns the corresponding result identifier */function Query ($sql) {if ($query = @mssql_query ($sql, $this->link)) {$this->qu      erynum++;    return $query;      } else {$this->querynum++;    $this->halt (' MSSQL Query Error ', $sql); }/* Executes the INSERT INTO statement and returns the auto-grow id*/function Insert ($table, $iarr) {$value = $this->insertsql ($iar) resulting from the last insert operation    R); $query = $this->query (' INSERT into ' $table. ' ' . $value. '; SELECT scope_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->updatesql (    $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) {$condition = ' WHERE '.    $condition; } $query = $this->query (' DELETE '. $table. $condition. ';    SELECT @ @ROWCOUNT as [ROWCOUNT]; ');    $record = $this->getrow ($query);    $this->clear ($query);  return $record [' rowcount ']; }/* Converts characters to MSSQL values that can be safely saved, such as a ' a ' to a ' a*/function EnCode ($str) {return str_replace ("', '" ', Str_replace (",", $str))  ; }  /* 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 the corresponding columns and values, such as: Array (' id ' = = 1, ' name ' = = ' name ') returned ([id], [name]) VALUES (1, ' name ') */function Insert      SQL ($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 '; }/* will generate corresponding INSERT statements for the corresponding columns and values, such as: Array (' id ' = = 1, ' name ' = ' name ') return [id] = 1, [name] = ' name ' */function Updatesql ($      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 row of the result of the corresponding query identity */function GetRow ($query, $result _type = Mssql_assoc) {return Mssql_fetch_array ($query, $resu  Lt_type);  }/* Clears the memory resource occupied by the query result */function Clear ($query) {return mssql_free_result ($query);  }/* Close 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"); }}?>

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.