phpオブジェクト向けの単一テーブル操作クラス


<?php
//        
define("HOST","localhost");
define("USER","root");
define("PASS","");
define("DBNAME","lamp78");
class Db
{
    protected $link;   //    
    protected $tablename; //  
    protected $pk='id'; //     
    protected $fields=array(); //        
    protected $where=array();//        
    protected $order=null; //    
    protected $limit=null; //    (      )
      
      
    public function __construct($tablename)
    {
        $this->tablename = $tablename;
        //      
        $this->link=@mysql_connect(HOST,USER,PASS) or die('       !');
        mysql_set_charset("utf8");
        mysql_select_db(DBNAME,$this->link);
        //            
        $this->getFields();
          
    }
      
    //    ,             
    private function getFields(){
        $sql = "desc {$this->tablename}";
        $result = mysql_query($sql,$this->link);
        //       
        while($row = mysql_fetch_assoc($result)){
            $this->fields[]=$row['Field']; //     
            //        
            if($row['Key']=="PRI"){
                $this->pk = $row['Field'];
            }
        }
        mysql_free_result($result);
    }
      
     //                 
    public function select()
    {
      $list = array();
      //  sql  
      $sql ="select * from {$this->tablename}";
        
      //     where  
      if(count($this->where)>0){
        $sql.=" where ".implode(" and ",$this->where);
      }
        
      //     order    
      if($this->order){
        $sql.=" order by ".$this->order;
      }
        
      //         
      if($this->limit){
        $sql.=" limit ".$this->limit;
      }
      //    ,    。
      $result = mysql_query($sql,$this->link);
      echo $sql;
      //     
      while($row = mysql_fetch_assoc($result)){
        $list[]=$row;
      }
      mysql_free_result($result); //     
      return $list;
    }
      
    //            
    public function total()
    {
      $list = array();
      //  sql  
      $sql ="select count(*) from {$this->tablename}";
        
      //     where  
      if(count($this->where)>0){
        $sql.=" where ".implode(" and ",$this->where);
      }
       
      //    ,    。
      $result = mysql_query($sql,$this->link);
        
      return mysql_result($result,0,0);
    }
      
    //      
    public function findAll()
    { $list = array();
      //  sql  
      $sql ="select * from {$this->tablename}";
      //    ,    。
      $result = mysql_query($sql,$this->link);
      //     
      while($row = mysql_fetch_assoc($result)){
        $list[]=$row;
      }
      mysql_free_result($result); //     
      return $list;
    }
      
    /**
     *     id        
     * @param int id        id 。
     * @return array    ,         ,    null
     */
    public function find($id)
    {
        $sql = "select * from {$this->tablename} where {$this->pk}={$id}";
        //    
        $result = mysql_query($sql,$this->link);
        //      
        if($result && mysql_num_rows($result)>0){
            return mysql_fetch_assoc($result);
        }else{
            return null;
        }
    }
      
    //    
    public function delete($id)
    {
        $sql = "delete from {$this->tablename} where {$this->pk}={$id}";
        //    
        mysql_query($sql,$this->link);
          
        //      
        return mysql_affected_rows($this->link);
    }
      
    //      
    public function insert($data=array()){
        //        ,       post    
        if(empty($data)){
            $data = $_POST;
        }
        //      
        $fieldlist = array();
        $valuelist = array();
        foreach($data as $k=>$v){
            //         
            if(in_array($k,$this->fields)){
                $fieldlist[] = $k;
                $valuelist[] = $v;
            }
        }
        //  sql  
        $sql = "insert into {$this->tablename}(".implode(",",$fieldlist).") values('".implode("','",$valuelist)."')";
        echo $sql;
        //    
        mysql_query($sql,$this->link);
          
        //      
        return mysql_insert_id($this->link);
    }
      
    //      
    public function update($data=array()){
        //        ,       post    
        if(empty($data)){
            $data = $_POST;
        }
        //      
        $fieldlist = array();
        foreach($data as $k=>$v){
            //         
            if(in_array($k,$this->fields) && $k!=$this->pk){
                $fieldlist[] = "{$k}='{$v}'";
            }
        }
        //  sql  
        $sql = "update {$this->tablename} set ".implode(",",$fieldlist)." where {$this->pk}=".$_POST[$this->pk];
        echo $sql;
        //    
        mysql_query($sql,$this->link);
          
        //      
        return mysql_insert_id($this->link);
    }
      
   //        
   public function where($data)
   {
        $this->where[]=$data;
        return $this;
   }
   //      
   public function order($data)
   {
        $this->order=$data;
        return $this;
   }
   //      
   public function limit($m,$n=0)
   {
        if($n>0){
            $this->limit = $m.",".$n;
        }else{
            $this->limit = $m;
        }
        return $this;
   }
}

本文は“夢の翼”のブログから出て、転載して作者と連絡してください!