【PHP】全自動安裝資料庫,不需要手動匯入
阿新 • • 發佈:2018-11-27
author:咔咔
wechat:fangkangfk
用了很多的自動安裝資料庫的專案了,今天也來寫一個,只是一個簡單版本,可以按照這個思路來建立適合自己專案的安裝資料庫
原始碼地址問價:
https://download.csdn.net/download/fangkang7/10799886
首先建立倆個檔案
doAction.php用來寫入資料庫資訊和建立資料庫操作
dbconfig.php用來儲存資料庫資訊
我們可以先看看資料庫配置的寫法,這裡沒有使用return
下來我們開始在obaction.php生成資料庫配置資訊
這裡有三個函式,簡單的解釋一下
is_writable()判斷檔案是否可以寫入
fopen()開啟一個檔案
fwrite()將資訊寫進一個開啟的檔案中
我們在看看有沒有寫入
通過fwrite()就可以生成$config的配置資訊
下來就是連線資料庫建立表
寫sql檔案
下來就開始將檔案寫入資料庫
由於myqli_query()不支援多條同時插入,所以我們就需要使用迴圈來插入資料庫
我們可以看看資料庫
建立的資料庫和自定義的表就ok了
我們就以install.lock為判斷點,當這個檔案存在時,就需要在進行安裝,直接跳轉到您已經安裝過的頁面
那麼這個檔案就在資料庫導 成功之後建立
我們在安裝一次,ok
這個功能最複雜的就是資料庫的sql檔案寫入,其他的按照自己的專案需求來寫即可
來一份原始碼:
<?php
$filename="dbconfig.php";
$fileLock = 'install.lock';
//配置檔案內容
$config='<?php';
$config.="\n";
$config.='$host="'.$_POST["host"].'";';
$config.="\n";
$config.='$user="'.$_POST["username"].'";';
$config.="\n";
$config.='$pass="'.$_POST["password"].'";';
$config.="\n";
$config.='$dbname="'.$_POST["dbname"].'";';
$config.="\n";
$config.='$flag="'.$_POST["flag"].'";';
$config.="\n";
$config.="?>";
if(file_exists($fileLock)){
echo "<script>window.location='ok.php';</script>";
die;
}
if(is_writable($filename)){//檢測是否有許可權可寫
$handle=fopen($filename, "w+");
fwrite($handle, $config);
// //連線資料庫
include_once($filename);
if( [email protected]$link=mysqli_connect($host,$user,$pass)){
echo "資料庫連線失敗,<a href='install.php'>返回設定</a>";
}else{
mysqli_query($link,"create database if not exists `$dbname`");
mysqli_select_db($link,$dbname);
//建表語句
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."access` (
`role_id` smallint(6) unsigned NOT NULL,
`node_id` smallint(6) unsigned NOT NULL,
`level` tinyint(1) NOT NULL,
`module` varchar(50) DEFAULT NULL,
KEY `groupId` (`role_id`),
KEY `nodeId` (`node_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."node` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`title` varchar(50) DEFAULT NULL,
`status` tinyint(1) DEFAULT '0',
`remark` varchar(255) DEFAULT NULL,
`sort` smallint(6) unsigned DEFAULT NULL,
`pid` smallint(6) unsigned NOT NULL,
`level` tinyint(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `level` (`level`),
KEY `pid` (`pid`),
KEY `status` (`status`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."role` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`pid` smallint(6) DEFAULT NULL,
`status` tinyint(1) unsigned DEFAULT NULL,
`remark` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`),
KEY `status` (`status`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$sql[]="CREATE TABLE IF NOT EXISTS `".$flag."role_user` (
`role_id` mediumint(9) unsigned DEFAULT NULL,
`user_id` char(32) DEFAULT NULL,
KEY `group_id` (`role_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
foreach ($sql as $value) {
mysqli_query($link,$value);
}
echo "<script>window.location='index.php';</script>";
$myfile = fopen("install.lock", "w");
}
}else{
echo "您沒有許可權操作。";
}