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!