thinkphp資料庫修改
在項目文件夾裡面的Conf的config.php裡面去配置就可以了,這個是公共配置文件。參考:
return array(
//'配置項'=>'配置值'
'APP_GROUP_LIST' => 'Index,Admin', //獨立分組
'DEFAULT_GROUP' => 'Index',
'APP_GROUP_MODE' => 1,
'APP_GROUP_PATH' => 'rzxt',
'DB_HOST' => '127.0.0.1',
'DB_USER' => 'root',
'DB_PWD' => '123456',
'DB_NAME' => 'klrz',
'DB_PREFIX' => 'kl_',
//'TMPL_VAR_IDENTIFY' => 'array',
//'TMPL_FILE_DEPR' => '_',
'DEFAULT_TIMEZONE'=>'Asia/Shanghai'
);
❷ thinkphp 怎麼實現對mysql做到創建表,修改欄位,添加欄位,刪除欄位
<?php
class MysqlManage{
/*創建資料庫,並且主鍵是aid
* table 要查詢的表名
*/
function createTable($table){
$sql="CREATE TABLE IF NOT EXISTS `$table` (`aid` INT NOT NULL primary key)ENGINE = InnoDB;";
M()->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;
}
}