PHP Package of MSSQL operation class complete instance, PHP package MSSQL instance
The example in this paper describes the MSSQL operation class in PHP encapsulation. Share to everyone for your reference, 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. = '
MSSQL Error: '. Mssql_get_last_message (); if ($sql) {$sql = '
sql: '. $sql; } exit ("DataBase Error.
Message $message $sql "); }}?>
More interested in PHP related content readers can view this site topic: "PHP based on PDO operation database Tips Summary", "PHP+MONGODB Database operation Skills Daquan", "PHP Object-oriented Programming tutorial", "PHP String Usage Summary", " Php+mysql database Operation Tutorial "and" PHP common database Operation Skills Summary "
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1133083.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133083.html techarticle PHP Packaged MSSQL operation class complete instance, PHP package MSSQL Example of this article describes the PHP package of MSSQL Operation class. Share to everyone for your reference, as follows: Php/*mssql Operation class ...