yii2中判斷數據表是否存在數據庫中(原創)


分為兩步:

第一步,找出數據庫中所有表名,表名得到的是二維數組。

第二步,判斷表名是否存在二維數組中

 

下面就貼我的代碼咯。

   $table_name =‘table’;
        $juge = $handle->createCommand("show tables ")->queryAll();
//下面的deep_in_array()方法是自己寫的方法,判斷是否存在值是否存在二維數組中,yii2中調用本類方法,可以去掉action
$cun = $this->deep_in_array($table_name,$juge); if(!$cun){ echo json_encode("nodata"); return; }

 

   //判斷二維數組是否存在值
    public  function deep_in_array($value, $array) {   
            foreach($array as $item) {   
                if(!is_array($item)) {   
                    if ($item == $value) {  
                        return true;  
                    } else {  
                        continue;   
                    }  
                }   
                   
                if(in_array($value, $item)) {  
                    return true;      
                } else if($this->deep_in_array($value, $item)) {  
                    return true;      
                }  
            }   
            return false;   
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM