<?php
class dbex
{
	 var $rowCount;
	 var $perPage;
	 var $currPage;
	 var $totalPage;
	 
	 public function setPages($perPage,$currPage=1)
   {
   	  if(empty($currPage)){ $currPage=1; }
   	  $this->perPage=$perPage;
   	  $this->currPage=$currPage;
   }

	public function setNopages()
   {
   		$this->perPage='';
   }
   
   public function getRs($sql)
   {
   	 $rs=array();
   	 $result=mysql_query($sql) or die('<script type="text/javascript">location.href="servtools/error.php?error_type=dberr";</script>');
   	 $this->rowCount=mysql_num_rows($result);
    
     if(empty($this->perPage)){
     	   while($rsRow=mysql_fetch_array($result)){
		   	 	   $rs[]=$rsRow;
		   }
     }else{
     	   $total_rs=$this->rowCount;
     	   $per_page=$this->perPage;
     	   $curr_page=$this->currPage;
     	   if($total_rs%$per_page==0){
     	   	  $total_page=$total_rs/$per_page;
     	   	  if($total_page==0){ $total_page=1; }
     	   }else{
     	   	  $total_page=(int)($total_rs/$per_page)+1;
     	   }
     	   
     	   //限制超页错误
     	   if($curr_page>$total_page){
     	   	  echo '<script type="text/javascript">history.go(-1);</script>';
     	   	  exit;
     	   }
     	   
     	   $start_row_num=($curr_page-1)*$per_page+1;
     	   $end_row_num=$curr_page*$per_page;
     	   if($curr_page==$total_page){
     	   	  $end_row_num=$total_rs;
     	   }
     	   $this->totalPage=$total_page;
     	   for($i=1;$i<=$end_row_num;$i++){
     	   	  $rsRow=mysql_fetch_array($result);
     	   	  if($i>=$start_row_num){
		   	 	      $rs[]=$rsRow;
		   	 	  }
		     }
		 }
     return $rs;
   }
   
   public function getALL($sql){
   	$this->setNopages();
   	return $this->getRs($sql);
  }   

   public function getRow($sql)
   {
   	 $result=mysql_query($sql) or die('<script type="text/javascript">location.href="servtools/error.php?error_type=dberr";</script>');
      return mysql_fetch_array($result);
   }

   public function exeUpdate($sql)
   {
      if(mysql_query($sql)){
      	return true;
      }else{
      	return false;
      }
   }
   
   public function create($sql)
   {
      $result=mysql_query($sql);
      $create=mysql_fetch_array($result);
      return $create;
   }
   
   public function affected_rows($sql){
   		return mysql_affected_rows();
  }
}
?>