PHP encapsulates a complete instance of MSSql operations, and php encapsulates mssql instances. PHP-encapsulated full MSSql operation class instance. php-encapsulated mssql instance this article describes the PHP-encapsulated MSSql operation class. For your reference, the details are as follows: php * MSSql operation class full instance of MSSql operation class encapsulated by PHP and mssql instance encapsulated by php
This example describes the MSSql operation class encapsulated by PHP. We will share this with you for your reference. The details are as follows:
<? Php/* MSSql operation class */class MSSql {var $ link; var $ querynum = 0;/* connect to the 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 ');}} /* execute the SQL statement and return the corresponding result ID */function Query ($ SQL) {if ($ query = @ mssql_query ($ SQL, $ this-> link )) {$ this-> querynum ++; return $ query;} else {$ this-> querynum ++; $ this-> halt ('mssql Query error ', $ SQL) ;}}/* execute the Insert Into statement and return the automatically increasing id generated by the last insert operation */function Insert ($ table, $ iarr) {$ value = $ this-> InsertSql ($ iarr); $ query = $ this-> Query ('Insert '. $ table. ''. $ value. '; SELECT SCOPE_IDENTITY () AS [insertid];'); $ record = $ this-> GetRow ($ query); $ this-> Clear ($ query ); return $ record ['insertid'];}/* execute the Update statement and return 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'];}/* execute the Delete statement and return 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'];}/* convert the character to a securely saved mssql value. for example, convert A' a to A' a */function EnCode ($ str) {return str_replace (''', ''', str_replace ('','', $ str ));} /* convert the securely saved mssql value to a normal value. for example, convert A' a to A' a */function DeCode ($ str) {return str_replace ('''', ''', $ str);}/* generates the corresponding insert statement for the corresponding columns and values, such as array ('id' => 1, 'name' => 'name') returns ([id], [name]) VALUES (1, 'name') */function InsertSql ($ 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 '';}/* generates the corresponding insert statement for the corresponding columns and values, for example: array ('id' => 1, 'name' => 'name') returns [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 identifier */function GetRow ($ query, $ result_type = MSSQL_ASSOC) {return mssql_fetch_array ($ query, $ result_type);}/* Clear the memory resources occupied by the query results */function Clear ($ query) {return mssql_free_result ($ query );} /* Close the database */function Close () {return mssql_close ($ this-> link);} function halt ($ message = '', $ SQL = '') {$ message. ='
MSSql Error: '. mssql_get_last_message (); if ($ SQL) {$ SQL ='
SQL: '. $ SQL;} exit ("DataBase Error.
Message $ message $ SQL ") ;}}?>