最新PHP SQL Server 数据库操作类
以下是三零网为大家整理的最新PHP SQL Server 数据库操作类的文章,希望大家能够喜欢!
PHP MsSql操作类,可实现连接MsSQL数据库、执行SQL语句、Insert Into、Update、Delete等相关操作。
class MSSql { var $link; var $querynum = 0; 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'); } } function Query($sql) { if($query = @mssql_query($sql, $this->link)) { $this->querynum++; return $query; } else { $this->querynum++; $this->halt('MSSQL Query Error', $sql); } } function Insert($table, $iarr) { $value = $this->InsertSql($iarr); $query = $this->Query('INSERT INTO ' . $table . ' ' . $value . '; SELECT SCOPE_IDENTITY() AS [insertid];'); $record = $this->GetRow($query); $this->Clear($query); return $record['insertid']; } 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']; } 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']; } function EnCode($str) { return str_replace("'","''", $str); } function DeCode($str) { return str_replace("''", "'", $str); } 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 ''; } } else { return ''; } } 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 ''; } } 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 = '', $sql = '') { $message .= ' MSSql Error:' . mssql_get_last_message(); if ($sql) { $sql = ' sql:' . $sql; } exit("Database Error. Message:$message $sql"); } } ?> |