/**
* PHP操作mssql数据库类,含分页类
* Enter description here ...
* @author aboc 9986584 2011-04-09
*
*/
class DB_Handle{
var $ClassName = "DB_Handle";
var $Server;
var $UserName;
var $Password;
var $Database;
var $LinkID=0;
var $QueryResult="";
var $LastInsertID = "";
//* private ignore=>ignore the error and continue, halt=>report the error and halt, report=>report the error and continue
var $Halt_On_Error = "report";
var $Error = "";
var $ErrNo = 0;
var $total = 0;
var $page = 0;
var $page_num = 10;
var $page_size = 10;
var $page_name = 'page';
//* remark: This is the db_mysql_class's structure * function: Set the server,username,password,database variable.
function DB_Handle($server="",$username="",$password="",$database=""){
$this->Server = $server;
$this->UserName = $username;
$this->Password = $password;
$this->Database = $database;
}
//* function: Connect database and select database * success: retun 1 * failed: return 0
function connect(){
$this->LinkID = @mssql_pconnect($this->Server,$this->UserName,$this->Password);
if(!$this->LinkID){
$this->halt("mssql_pconnect($this->Server,$this->UserName,$this->Password): Failed");
return 0;
}
if(!@mssql_select_db($this->Database)){
$this->halt("mssql_select_db($this->Database) Failed.");
return 0;
}
return 1;
}
//* function: Check the database, if exist then select * exist: return 1 * not exist: return 0
function selectDatabase(){
if(@mssql_select_db($this->Database))
return 1;
else
return 0;
}
//* function: Execute SQL instruction * success: return SQL Result. * failed: return 0;
function execQuery($sql=""){
if($this->LinkID == 0){
$this->halt("Execute SQL Failed: Hava not valid database connect.");
return 0;
}
ob_start();
$this->QueryResult = mssql_query($sql,$this->LinkID);
$error = ob_get_contents();
ob_end_clean();
if($error){
$this->halt("Execute SQL: mssql_query($sql,$this->LinkID) failed.");
return 0;
}
$reg = "#insert into#";
if(preg_match($reg,$sql)){
$sql = "select @@IDENTITY as id";
$res = mssql_query($sql,$this->LinkID);
$this->LastInsertID = mssql_result($res,0,id);
}
return $this->QueryResult;
}
//* function: Get the query result's row number//* success: return the row fo the Result//* failed: return 0
function getTotalRowNum($result=""){
if($result != "") $this->QueryResult = $result;
$row = @mssql_num_rows($this->QueryResult);
if($row >= 0) return $row;
$this->halt("Get a row of result Failed: Result $result is invalid.");
return 0;
}
//* function: Get the last insert record's id //* success: return id //* failed: return 0
function lastInsertID(){
return $this->LastInsertID;
}
//* function: Get a field's value * success: return value of the field * failed: return 0
function getField($result="",$row=0,$field=0){
if($result != "") $this->QueryResult = $result;
$fieldvalue = @mssql_result($this->QueryResult,$row,$field);
if($fieldvalue != "") return $fieldvalue;
$this->halt("Get field: mssql_result($this->QueryResult,$row,$field) failed.");
return 0;
//Here should have error handle
}
//* function: Get next record * success: return a array of the record's value * failed: return 0
function nextRecord($result=""){
if($result != "") $this->QueryResult = $result;
$record = @mssql_fetch_array($this->QueryResult);
if(is_array($record)){
foreach ($record as $key=>$row)
$record[$key] = trim($row);
return $record;
}
//$this->halt("Get the next record Failed: the Result $result is invalid.");
return 0;
}
//* function: Free the Query Result * success return 1 * failed: return 0
function freeResult($result=""){
if($result != "") $this->QueryResult = $result;
return @mssql_free_result($this->QueryResult);
}
//* function: Set the Halt_On_Error's state * success: return 1 * failed: return 0
function setHaltOnError($state="ignore"){
if(!($state == "ignore" || $state == "report" || $state == "halt")){
$this->halt("Set the Halt_On_Error Fail: There is no state value $state");
return 0;
}
$this->Halt_On_Error = $state;
return 1;
}
//* function: Get the Halt_On_Error's state
function getHaltOnError(){
return $this->Halt_On_Error;
}
//* function: Get the class's name
function toString(){
return $this->ClassName;
}
//* function: Error handle
function halt($msg){
$this->Error = @mysql_error($this->LinkID);
$this->ErrNo = @mysql_errno($this->LinkID);
if ($this->Halt_On_Error == "ignore") return;
$this->makeMsg($msg);
if ($this->Halt_On_Error == "halt") die("Session halted");
}
//* function: Make error information and print
function makeMsg($msg){
printf("Database error: %s
\n", $msg);
printf("MySQL Error: %s (%s)
\n",$this->ErrNo,$this->Error);
}
/**
* 以下为分页类
*/
private function getQuery(){
$query = '';
if( isset($_GET) && is_array($_GET)){
foreach ($_GET as $key=>$value){
if ($key == 'page')continue;
$query .= !empty($query)?'&'.$key.'='.$value:$key.'='.$value;
}
}
$query .= empty($query)?'':'&';
return $this->getFilename().$query;
}
/**
* 获取当前的url文件名并带上?
*/
private function getFilename() {
//$filename = str_replace($_SERVER['QUERY_STRING'],'',basename($_SERVER["REQUEST_URI"]));
$filename = str_replace('?'.$_SERVER['QUERY_STRING'],'',basename($_SERVER["REQUEST_URI"])).'?';
return $filename;
}
/**
* 返回带分页名的连接字符
*/
private function returnPageLink( $page ){
return $this->getQuery().$this->page_name.'='.$page;
}
/**
* 下一页链接
*/
function nextPageLink(){
if( $this->page != $this->page_num )
return $this->returnPageLink($this->page+1);
else
return '#';
}
function getListData($sql,$num=10){
$this->execQuery($sql);
$this->page_num = $this->getTotalRowNum();
if (!$this->page_num || !$num )return array();
$this->page_size = $num;
$this->page = isset($_GET[$this->page_name])?intval($_GET[$this->page_name]):1;
$pageTotal = ceil($this->page_num/$num);
if ($this->page < 0)$this->page = 0;
if ($this->page > $pageTotal) $this->page = $pageTotal;
$list = array();
if (mssql_data_seek($this->QueryResult, ($this->page-1)*$this->page_size)){
for ($i=0;$i
$list[] = $this->nextRecord();
}
}
return $list;
}
/**
* 输出中文分页链接 上一页,下一页
*
* @return unknown
*/
function pagelist( $total='总共',$dataname='条',$now='当前',$page='页',$first='第一页',$last='尾页',$prev='上一页',$next='下一页' ){
$total=''.$total.$this->page_size.$dataname.' '.$now.($this->page).'/'.($this->page_num).$page.'';
$startpage=''.$first.'';
$endpage=''.$last.'';
if($this->page == 1){
$prepage=''.$prev.'';
}else{
$prepage=''.$prev.'';
}
if($this->page==$this->page_num){
$nextpage=''.$next.'';
}else{
$nextpage=''.$next.'';
}
if($this->page_size == 0)return '';
return $total.' '.$startpage.' '.$prepage.' '.$nextpage.' '.$endpage;
}
/**
* 输出下拉框的列表
*
*/
function select() {
$select = '';
$totalnum = $this->page_num;
for($i=1;$i<=$totalnum;$i++) {
if($this->page == $i){
$str = ' selected="selected"';
} else {
$str = '';
}
$select .= ''.$i.'';
}
$select .= '';
if($this->page_size == 0)$select = '';
return $select;
}
/**
* 输入页数到多少页
*
*/
function gotoPage($buttonname='到'){
$string = ' ';
if($this->page_size == 0)$string = '';
return $string;
}
function pageListBaidu($first='首页',$pre_page='上一页',$next_page='下一页',$last='尾页' ,$shownum=6) {
if($this->page_num == 1) return;
$pagelink = '';
if($this->page != 1) {
$pagelink .= '['.$first.'] ';
$pagelink .= '['.$pre_page.'] ';
}
for ($i=$shownum;$i>=1;$i--) {
$tmp_page = $this->page - $i;
if( $tmp_page >= 1 ) {
$pagelink .= '['.$tmp_page.'] ';
}
}
$pagelink .= ''.$this->page.'';
for ($i=1;$i<=$shownum;$i++) {
$tmp_page = $this->page + $i;
if( $tmp_page <= $this->page_num ) {
$pagelink .= '['.$tmp_page.'] ';
}
}
if( $this->page != $this->page_num ){
$pagelink .= '['.$next_page.'] ';
$pagelink .= '['.$last.']';
}
if($this->num == 0)$pagelink = '';
return $pagelink;
}
}