當前位置:首頁 » 操作系統 » thinkphp資料庫修改

thinkphp資料庫修改

發布時間: 2023-06-07 20:05:51

❶ 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;
}
}

熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:619
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:347
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:62
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:288
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:781
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:334
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:197
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:791
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:350
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:578