工具类 mysql操作类

来源:互联网 发布:软件定义存储 编辑:程序博客网 时间:2024/05/24 06:36

<?

 

 /**
 **  v1.1 版本  mysql操作类
 */

 

 


class DB{
 
 protected $db_server;
 protected $db_database;
 protected $db_user;
 protected $db_password;
 protected $charset;
 protected $pconnect;
 protected $debug;
 protected $_conn;
 
 
  //设置一些参数
 function  setCharset($charset)
 {
    if(!empty($charset))  $this->charset=$charset;
 
 
 
 }
 function setDbServer($db_server)
 {
   if(!empty($db_server))$this->db_server = $db_server;
 }

 function setDataBase($db_database)
 {
   if(!empty($db_database))$this->db_database = $db_database;
 }

 function setDbUser($db_user)
 {
   if(!empty($db_user))$this->db_user = $db_user;
 }

 function setDbPassword($db_password)
 {
   if(!empty($db_password))$this->db_password = $db_password;
 }

 function setDebug($debug)
 {
   if($debug)$this->debug =1;
 }

 function setPconnect($pconnect)
 {
   if($pconnect) $this->pconnect = 1;
 }
    function outError()//开启调试
    {
   if($this->debug && mysql_errno())
   echo mysql_errno().":".mysql_error();
 }

 
 /**
 **初始化函数
 */
 function __construct($db_server = "", $db_user = "", $db_password = "", $db_database = "",$char="",$pconnect = 0)
                {
                        $this->setDbServer($db_server);
                        $this->setDataBase($db_database);
                        $this->setDbUser($db_user);
                        $this->setDbPassword($db_password);
                        $this->setPconnect($pconnect);
      $this->setCharset($char);
     
                }
    
  function Connection(){
        if($this->pconnect)
      {
       $conn=mysql_pconnect($this->db_server, $this->db_user, $this->db_password) or die("无法连接数据库");
     mysql_select_db($this->db_database, $conn)or die('无法连接数据库'.$this->db_database.mysql_error());
      mysql_query("set  names ".$this->charset); 
      $this->_conn=$conn;
      }   
      else{
      $conn= mysql_connect($this->db_server, $this->db_user, $this->db_password) or die("无法连接数据库");
      mysql_select_db($this->db_database, $conn)or die('无法连接数据库'.$this->db_database.mysql_error());
    mysql_query("set  names ".$this->charset); 
    $this->_conn=$conn;
      } 
   $this->outError();  
     }   
 /**
 **用途:从数据库中删除一条数据
 **return:bool true/false
 */
 public function del($sql){
  if(!$this->_conn)$this->Connection();
    $result = mysql_query($sql);
    return $result;
  }
 /**
 **用途:修改数据库中的一条数据
 **return:bool true/false
 */
 public function update($sql){
  if(!$this->_conn)$this->Connection();
    $result = mysql_query($sql);
    return $result;
  }
 /**
 **用途:从数据库中插入一条数据
 **return:成功返回最后id失败返回false
 */
 public function insert($sql){
  if(!$this->_conn)$this->Connection();
    $result = mysql_query($sql);
    if(!$result)return false;
    $lastId = mysql_insert_id();
  return $lastId;
  }
 /**
 **用途:从数据库中选出一条数据
 **return:一维的array
 */
 public function fetchRow($sql){
  if(!$this->_conn)$this->Connection();
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);
    return $row;
  }
 
 /**
 **用途:从数据库中选出所有数据
 **return:二维的array
 */  
 public function fetchAll($sql){
  if(!$this->_conn)$this->Connection();
    $result = mysql_query($sql);
    //mysql_data_seek($result, 0);
    while ($row = mysql_fetch_assoc($result))
    {
        $output[] = $row;
    }
    mysql_free_result($result);
    return $output;
  }
 
 /**
 **用途:获取表中数据的个数
 **pram $table(表名)
 **return int
 */
 public function getTotal($table){
  $sql="select * from '$table';";
  if(!$this->_conn)$this->connect();
    $result = mysql_query($sql);
    $row = mysql_num_rows($result);
    return $row;
  }
 /**
 **析构函数关闭数据库连接
 */
 public function getInfo(){
  echo "这是MysqlDb类!";
  }
 public function __distruct(){
  if($this->_conn)mysql_close();
  }
    public  function substrGB($in,$num,$charset){
     //$num=16;
     $pos=0;
     $byteNum=0;
     $out="";
     while($num){
     $c=mb_substr($in,$pos,1,$charset);
     if($c=="/n") break;
     if(strlen($c)==1){
        $pos++;
        $byteNum++;
        if($byteNum>$num) break;
         $out.=$c;
      }
      else{
          $pos++;
           $byteNum=$byteNum+2;
          if($byteNum>$num) break;
          $out.=$c;
        }
       }
       return $out."...";
 }
 
 /**
 **   这是一个截取字符串的函数,其中 参数$arr 是要截取的字符串,$str 是数据库中的字段名,$restr 是截取之后的字段名称,$min 是当字符串超过这个长度就截取, $length 截取的长度,$char 是截取字符串的编码
 */
    public  function strgb($arr,$str,$restr,$min,$length,$char){
  $i=0;
  while($arr[$i]){
   if(strlen($arr[$i][$str])>$min){
    $arr[$i][$restr]=$this->substrGB($arr[$i][$str],$length,$char);

   }
   else $arr[$i][$restr]=$arr[$i][$str];
   $i++;
  }
  return $arr;
 } 
  
  
  
 }
?>

原创粉丝点击