thinkphp6操作数据库——判断数据表、字段是否存在,添加、修改、删除字段

发布于:2023-04-14 11:37:36

之前使用thinkphp6写了一个网站,但是没有写后台在线更新的功能,每次更新都需要下载新的文件覆盖,数据表还得自己手动创建,给没有相关知识的人去使用就会比较难,所以我决定开发一个在线更新的功能。


封装了一些操作数据表的方法,方便创建和修改数据表字段,代码如下:


判断数据表是否存在

   public function check_table($table){

        $res = Db::query('SHOW TABLES LIKE '."'".$table."'");

        if($res){

            return 1;

        }else{

            return 0;

        }

    }

   public function check_table($table){
        $res = Db::query('SHOW TABLES LIKE '."'".$table."'");
        if($res){
            return 1;
        }else{
            return 0;
        }
    }


判断字段是否存在

   public function check_column($table,$column){
        $res = Db::query('select count(*) from information_schema.columns where table_name = '."'".$table."' ". 'and column_name ='."'".$column."'");
        if($res[0]['count(*)'] != 0){
            return 1;
        }else{
            return 0;
        }
    }
    添加字段
    
    public function add_column($table,$column,$type,$condition,$after){
        $res = Db::execute('alter table'." `".$table."` ".'add'." `".$column."` ".$type." ".$condition." ".'after'." `".$after."`");
        if($res){
            return 1;
        }else{
            return 0;
        }
    }

https://blog.csdn.net/aa2325727631/article/details/128873958

阅读 356+

一片空白

父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。