php數據庫操作類

jopen 10年前發布 | 18K 次閱讀 PHP開發 PHP

    <?php
/==================================================================/
/ 文件名:BaseLogic.class.php /
/ 概要: 數據處理公共類. /

    class BaseLogic extends MyDB {  
        protected $tabName;     //表的名稱  
        protected $fieldList;   //字段集合  
        protected $messList;  

        //==========================================  
        // 函數: add($postList)  
        // 功能: 添加  
        // 參數: $postList 提交的變量列表  
        // 返回: 剛插入的自增ID  
        //==========================================  
        function add($postList) {  
            $fieldList='';  
            $value='';  
            foreach ($postList as $k=>$v) {  
                if(in_array($k, $this->fieldList)){  
                    $fieldList.=$k.",";  
                    if (!get_magic_quotes_gpc())  
                        $value .= "'".addslashes($v)."',";  
                    else  
                        $value .= "'".$v."',";  
                }  
            }  

            $fieldList=rtrim($fieldList, ",");  
            $value=rtrim($value, ",");  

            $sql = "INSERT INTO {$this->tabName} (".$fieldList.") VALUES(".$value.")";  
            echo $sql;  
            $result=$this->mysqli->query($sql);  
            if($result && $this->mysqli->affected_rows >0 )   
                return $this->mysqli->insert_id;  
            else  
                return false;  
        }  


        //==========================================  
        // 函數: mod($postList)  
        // 功能: 修改表數據  
        // 參數: $postList 提交的變量列表  
        //==========================================  
        function mod($postList) {  
            $id=$postList["id"];  
            unset($postList["id"]);  
            $value='';  
            foreach ($postList as $k=>$v) {  
                if(in_array($k, $this->fieldList)){  
                    if (!get_magic_quotes_gpc())  
                        $value .= $k." = '".addslashes($v)."',";  
                    else  
                        $value .= $k." = '".$v."',";  
                }  
            }  
            $value=rtrim($value, ",");  
            $sql = "UPDATE {$this->tabName} SET {$value} WHERE id={$id}";  
            return $this->mysqli->query($sql);      
        }  

        //==========================================  
        // 函數: del($id)  
        // 功能: 刪除  
        // 參數: $id 編號或ID列表數組  
        // 返回: 0 失敗 成功為刪除的記錄數  
        //==========================================  
        function del($id) {  
            if(is_array($id))  
                $tmp = "IN (" . join(",", $id) . ")";  
            else   
                $tmp = "= $id";  

            $sql = "DELETE FROM {$this->tabName} WHERE id " . $tmp ;  
            return $this->mysqli->query($sql);      

        }  


        function get($id) {  
            $sql = "SELECT * FROM {$this->tabName} WHERE id ={$id}";  

            $result=$this->mysqli->query($sql);  

            if($result && $result->num_rows ==1){  
                return $result->fetch_assoc();  
            }else{  
                return false;  
            }  

        }  
        function getMessList(){  
            $message="";  
            if(!empty($this->messList)){  
                foreach($this->messList as $value){  
                    $message.=$value."<br>";  
                }  
            }  
            return $message;      
        }  
    }  
?>  </pre><br />
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!