yii2判断表是否存在

yii2判断表是否存在

分为两步:        (推荐学习:yii教程

第一步,找出数据库中所有表名,表名得到的是二维数组。

第二步,判断表名是否存在二维数组中

 下面就贴我的代码咯。

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

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享