您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

ThinkPHP Mysql表结构修改类

2025/7/17 5:37:33发布36次查看
基于thinkphp的mysql表结构更改类,可以创建数据表,添加,编辑删除字段
execute($sql);
        $this->checktable($table);
    }
    /*
     * 检测表是否存在,也可以获取表中所有字段的信息
     * table 要查询的表名
     * return 表里所有字段的信息
     */
    function checktable($table){
        $sql=desc `$table`;
        $info=m()->execute($sql);
        return $info;
    }
/*
     * 检测字段是否存在,也可以获取字段信息(只能是一个字段)
     * table 表名
     * field 字段名
     */
    function checkfield($table,$field){
        $sql='desc `$table` $field';
        $info=m()->execute($sql);
        return $info;
    }
/*
     * 添加字段
     * table 表名
     * info  字段信息数组 array
     * return 字段信息 array
     */
    function addfield($table,$info){
        $sql=alter table `$table` add column;
        $sql.=$this->filterfieldinfo();
        m()->execute($sql);
        $this->checkfield($table,$info['name']);
    }
/*
     * 修改字段
     * 不能修改字段名称,只能修改
     */
    function editfield($table,$info){
        $sql=alter table `$table` modify ;
        $sql.=$this->filterfieldinfo($info);
        m()->execute($sql);
        $this->checkfield($table,$info['name']);
    }
/*
     * 字段信息数组处理,供添加更新字段时候使用
     * info[name]   字段名称
     * info[type]   字段类型
     * info[length]  字段长度
     * info[isnull]  是否为空
     * info['default']   字段默认值
     * info['comment']   字段备注
     */
    private function filterfieldinfo($info){
        if(!is_array($info))
            return
        $newinfo=array();
        $newinfo['name']=$info['name'];
        $newinfo['type']=$info['type'];
        switch($info['type']){
            case 'varchar':
            case 'char':
                $newinfo['length']=empty($info['length'])?100:$info['length'];
                $newinfo['isnull']=$info['isnull']==1?'null':'not null';
                $newinfo['default']=empty($info['default'])?'':'default '.$info['default'];
                $newinfo['comment']=empty($info['comment'])?'':'comment '.$info['comment'];
                                 break;
            case 'int':
                $newinfo['length']=empty($info['length'])?7:$info['length'];
                $newinfo['isnull']=$info['isnull']==1?'null':'not null';
                $newinfo['default']=empty($info['default'])?'':'default '.$info['default'];
                $newinfo['comment']=empty($info['comment'])?'':'comment '.$info['comment'];
                                 break;
            case 'text':
                $newinfo['length']='';
                $newinfo['isnull']=$info['isnull']==1?'null':'not null';
                $newinfo['default']='';
                $newinfo['comment']=empty($info['comment'])?'':'comment '.$info['comment'];
                                break;
        }
        $sql=$newinfo['name']. .$newinfo['type'];
        $sql.=(!empty($newinfo['length']))?($newinfo['length']) .' ':' ';
        $sql.=$newinfo['isnull'].' ';
        $sql.=$newinfo['default'];
        $sql.=$newinfo['comment'];
        return $sql;
    }
/*
     * 删除字段
     * 如果返回了字段信息则说明删除失败,返回false,则为删除成功
     */
    function dropfield($table,$field){
        $sql=alter table `$table` drop column $field;
        m()->execute($sql);
        $this->checkfield($table,$filed);
    }
/*
     * 获取指定表中指定字段的信息(多字段)
     */
    function getfieldinfo($table,$field){
        $info=array();
        if(is_string($field)){
            $this->checkfield($table,$field);
        }else{
            foreach($field as $v){
                $info[$v]=$this->checkfield($table,$v);
            }
        }
        return $info;
    }
}转载请注明出处:http://a3147972.blog.51cto.com/2366547/1543179
ad:真正免费,域名+虚机+企业邮箱=0元
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product