<?php
// Author: YangYu [email protected]
// msn: [email protected]
class cls_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 getAll($sql){
if (!($this->dbResult = $this->Query($sql))){
return false;
}
$this->dbRecord = array();
while ($row = @mssql_fetch_array($this->dbResult, MSSQL_ASSOC)) {
$this->dbRecord[] = $row;
}
@mssql_free_result($this->dbResult);
if (!is_array($this->dbRecord) || empty($this->dbRecord)){
return false;
}
return $this->dbRecord;
}
function getOne($sql){
if (!($this->dbResult = $this->Query($sql))){
return false;
}
$this->dbRecord = array();
$row = @mssql_fetch_array($this->dbResult, MSSQL_ASSOC);
@mssql_free_result($this->dbResult);
if (!is_array($row) || empty($row)){
return false;
}
$this->dbRecord = current($row);
return $this->dbRecord;
}
function getRow($sql){
if (!($this->dbResult = $this->Query($sql))){
return false;
}
$this->dbRecord = array();
$this->dbRecord = @mssql_fetch_array($this->dbResult, MSSQL_ASSOC);
@mssql_free_result($this->dbResult);
if (!is_array($this->dbRecord) || empty($this->dbRecord)){
return false;
}
return $this->dbRecord;
}
function Clear($query) {
return mssql_free_result($query);
}
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");
}
}
?>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yangyu112654374/archive/2009/05/26/4217557.aspx