后臺直接操作數據庫,實現新建、編輯、刪除數據表,新建、編輯、刪除數據庫字段,Thinkphp5數據庫管理操作源碼
分類:PHP
> Thinkphp
難易:初級
下載資源
下載積分:
300
積分
標簽:數據庫 操作 Thinkphp5 thinkphp1、使用前請配置虛擬主機;將整個文件放在根目錄下面;
2、數據庫導入;
3、使用框架:thinkphp5.1;
4、版本要求請根據框架要求的版本配置。
//拼接SQL語句
$sql = "CREATE TABLE IF NOT EXISTS `$tablename` (" ;
$key_num;
$numbers = 0;
foreach($fields as $key => $val){
if($val['length']==''){
$this->error('字段長度不能為空!','add','',1);
}
$name = $val['name'];
$type = $val['type'];
$notes= $val['notes'];
if(isset($val['key'])){
++$numbers;
$leng = $val['length'];
$sql .= " `$name` $type($leng) NOT NULL AUTO_INCREMENT COMMENT '$notes',";
$key_num = $key;
}else{
if($val['length']!=''){
$leng = $val['length'];
$sql .= " `$name` $type($leng) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '$notes',";
}else{
$sql .= " `$name` $type CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '$notes',";
}
}
}
if($numbers>1){
$this->error('主鍵只能有一個!','add','',1);
}else if($numbers==0){
$this->error('必須要選擇一個主鍵!','add','',1);
}
$key = $fields[$key_num]['name'];
$sql .= "PRIMARY KEY (`$key`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '$tablenotes' ROW_FORMAT = Dynamic;";
$creat = Db::execute($sql);