php 生成数据表函数

public function make_tab($data,$tab)
    {
		$table_name = $tab;
        $isTable = Db::query('SHOW TABLES LIKE ' . "'" . $table_name . "'");
        if (!empty($isTable)) {
            //表存在
            //echo 1;
            
        } else {
            //表不存在
            //echo 2;
			$str = '';
			foreach($data as $k=> $v){
				$thisstr = "`".$k."` varchar(200) NOT NULL DEFAULT '' COMMENT '".$v."',";
				$str = $str.$thisstr;
			}
            $sql = " CREATE TABLE IF NOT EXISTS `$table_name` (
				`id` int(11) NOT NULL AUTO_INCREMENT,
				`create_time` int(11) NOT NULL DEFAULT '0' COMMENT '创建和接收时间',
                `update_time` int(11) NOT NULL DEFAULT '0' COMMENT '更新时间',
				".$str."
				PRIMARY KEY (`id`)
				) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COMMENT='".$tab."信息';";
            Db::execute($sql);//执行SQL生成表
        }
        return $table_name;
    }
posted @ 2022-08-24 09:08  子岚天羽卿怜水  阅读(81)  评论(0编辑  收藏  举报