Home  >  Article  >  PHP Framework  >  How to determine whether the data table exists in the database in Yii

How to determine whether the data table exists in the database in Yii

王林
王林Original
2019-12-17 17:10:512723browse

How to determine whether the data table exists in the database in Yii

The judgment method is divided into two steps:

The first step: Find all the table names in the database. The table names are obtained as two-dimensional arrays.

Step 2: Determine whether the table name exists in the two-dimensional array.

Code example:

$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;   
        }

Recommended related articles and tutorials: yii tutorial

The above is the detailed content of How to determine whether the data table exists in the database in Yii. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn