Home >php教程 >php手册 >ThinkPHP Mysql表结构修改类

ThinkPHP Mysql表结构修改类

WBOY
WBOYOriginal
2016-06-07 11:41:141446browse

基于ThinkPHP的Mysql表结构更改类,可以创建数据表,添加,编辑删除字段
<?php <br /> /*<br>  *    mysql表结构处理类<br>  *    创建数据表,增加,编辑,删除表中字段<br>  *<br>  */<br> class MysqlManage{<br>     /*<br>      * 创建数据库,并且主键是aid<br>      * table 要查询的表名<br>      */<br>     function createTable($table){<br>         $sql="CREATE TABLE IF NOT EXISTS `$table` (`aid` INT NOT NULL primary key)ENGINE = InnoDB;";<br>         M()->execute($sql);<br>         $this->checkTable($table);<br>     }<br>     /*<br>      * 检测表是否存在,也可以获取表中所有字段的信息<br>      * table 要查询的表名<br>      * return 表里所有字段的信息<br>      */<br>     function checkTable($table){<br>         $sql="desc `$table`";<br>         $info=M()->execute($sql);<br>         return $info;<br>     }<br> <br>     /*<br>      * 检测字段是否存在,也可以获取字段信息(只能是一个字段)<br>      * table 表名<br>      * field 字段名<br>      */<br>     function checkField($table,$field){<br>         $sql='desc `$table` $field';<br>         $info=M()->execute($sql);<br>         return $info;<br>     }<br> <br>     /*<br>      * 添加字段<br>      * table 表名<br>      * info  字段信息数组 array<br>      * return 字段信息 array<br>      */<br>     function addField($table,$info){<br>         $sql="alter table `$table` add column";<br>         $sql.=$this->filterFieldInfo();<br>         M()->execute($sql);<br>         $this->checkField($table,$info['name']);<br>     }<br> <br>     /*<br>      * 修改字段<br>      * 不能修改字段名称,只能修改<br>      */<br>     function editField($table,$info){<br>         $sql="alter table `$table` modify ";<br>         $sql.=$this->filterFieldInfo($info);<br>         M()->execute($sql);<br>         $this->checkField($table,$info['name']);<br>     }<br> <br>     /*<br>      * 字段信息数组处理,供添加更新字段时候使用<br>      * info[name]   字段名称<br>      * info[type]   字段类型<br>      * info[length]  字段长度<br>      * info[isNull]  是否为空<br>      * info['default']   字段默认值<br>      * info['comment']   字段备注<br>      */<br>     private function filterFieldInfo($info){<br>         if(!is_array($info))<br>             return<br>         $newInfo=array();<br>         $newInfo['name']=$info['name'];<br>         $newInfo['type']=$info['type'];<br>         switch($info['type']){<br>             case 'varchar':<br>             case 'char':<br>                 $newInfo['length']=empty($info['length'])?100:$info['length'];<br>                 $newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL';<br>                 $newInfo['default']=empty($info['default'])?'':'DEFAULT '.$info['default'];<br>                 $newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment'];<br>                                  break;<br>             case 'int':<br>                 $newInfo['length']=empty($info['length'])?7:$info['length'];<br>                 $newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL';<br>                 $newInfo['default']=empty($info['default'])?'':'DEFAULT '.$info['default'];<br>                 $newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment'];<br>                                  break;<br>             case 'text':<br>                 $newInfo['length']='';<br>                 $newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL';<br>                 $newInfo['default']='';<br>                 $newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment'];<br>                                 break;<br>         }<br>         $sql=$newInfo['name']." ".$newInfo['type'];<br>         $sql.=(!empty($newInfo['length']))?($newInfo['length']) .' ':' ';<br>         $sql.=$newInfo['isNull'].' ';<br>         $sql.=$newInfo['default'];<br>         $sql.=$newInfo['comment'];<br>         return $sql;<br>     }<br> <br>     /*<br>      * 删除字段<br>      * 如果返回了字段信息则说明删除失败,返回false,则为删除成功<br>      */<br>     function dropField($table,$field){<br>         $sql="alter table `$table` drop column $field";<br>         M()->execute($sql);<br>         $this->checkField($table,$filed);<br>     }<br> <br>     /*<br>      * 获取指定表中指定字段的信息(多字段)<br>      */<br>     function getFieldInfo($table,$field){<br>         $info=array();<br>         if(is_string($field)){<br>             $this->checkField($table,$field);<br>         }else{<br>             foreach($field as $v){<br>                 $info[$v]=$this->checkField($table,$v);<br>             }<br>         }<br>         return $info;<br>     }<br> }转载请注明出处:http://a3147972.blog.51cto.com/2366547/1543179

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn