PHP实现Mysql数据库安装程序
时间:2007-01-05
来源:互联网
最近用PHP做了一个Mysql数据库安装程序,用过PHP程序的朋友们都应该知道第一步怎么做,除了把程序源码放到相应目录里外,第一步就是程序安装,安装时大多数都是运行目录下的install.php,安装后安装相关联文件即自动删除,并在配置文件中记录你在安装时的配置信息,下面我详细说明一下。
设计概要
设计一个安装界面,再根据所需安装步骤创建不同的安装过程,通过一个个表单的获取变量来保存在这些安装步骤窗体之间的参数值。安装数据库和初始化数据均使用SQL脚本来生成,这些脚本统一保存在一个定义好的PHP文件中(更好的选择当然是放到一个格式化好的XML文件中),安装程序通过配置文件中指定的路径及变量读取该PHP文件,安装成功后并在根据根目录中保存数据库信息(包括:数据库地址、数据库名称、数据库用户、数据库密码)config_db.php的文件,生成该文件后则表示安装程序已经成功,如果想要重新安装必须删除此文件。
安装程序通过配置文件中配置好的SQL脚本来创建数据库信息,如“会员表”、“分类表”、“系统频道参数表”、“统计信息表”、“短信表”,具体配置文件中SQL脚本内容如下:
代码:
//===================================================================//会员表
//===================================================================
$Asen_User_schema = "
create table Asen_User_table (
uid int(11) unsigned not null auto_increment,
User_class tinyint(2) default '0' not null,
User_id varchar(20) default '' not null,
User_pass varchar(32) default '' not null,
User_name varchar(30) default '' not null,
User_jumin varchar(13) default '' not null,
User_sex tinyint(2) default '0' not null,
User_birthday varchar(8),
User_tel1 varchar(20),
User_tel2 varchar(20),
User_tel3 varchar(20),
User_email varchar(150),
User_zip varchar(10),
User_address varchar(255),
User_homepage varchar(255),
User_automail tinyint(2) default '0' not null,
User_recommend varchar(20),
User_interest tinyint(2) default '0' not null,
User_hobby tinyint(2) default '0' not null,
User_religion tinyint(2) default '0' not null,
User_blood tinyint(2) default '0' not null,
User_job tinyint(2) default '0' not null,
User_jobname1 varchar(30),
User_jobname2 varchar(30),
User_tel4 varchar(20),
User_tel5 varchar(20),
User_jobzip varchar(10),
User_jobaddress varchar(255),
User_marriage tinyint(2) default '0' not null,
User_marriageday varchar(8),
User_introduction text,
User_main text,
User_admin_memo text,
User_point double(11,2) unsigned default '0.00' not null,
User_image varchar(255),
User_login int(11) unsigned default '0' not null,
modify_id varchar(20),
modify_ip varchar(20),
modify_date int(11) unsigned default '0' not null,
register_date int(11) unsigned default '0' not null,
approval_date int(11) unsigned default '0' not null,
primary key (uid),
key User_id (User_id),
key User_pass (User_pass),
key User_class (User_class),
key User_jumin (User_jumin),
key User_point (User_point),
key register_date (register_date)
)";
//===================================================================
//分类表
//===================================================================
$Asen_category_schema = "
create table Asen_category_table (
uid int(11) unsigned not null auto_increment,
category_id int(8) zerofill not null,
category_depth tinyint(2) default '1' not null,
category_name varchar(255),
category_skin varchar(255),
category_grant tinyint(2) default '0' not null,
category_file1 varchar(255),
category_file2 varchar(255),
category_rank int(11) unsigned default '1' not null,
use_html tinyint(2) default '0' not null,
category_head text,
category_foot text,
category_admin_memo text,
category_hit int(11) unsigned default '0' not null,
category_vote int(11) unsigned default '0' not null,
register_date int(11) unsigned default '0' not null,
approval_date int(11) unsigned default '0' not null,
primary key (uid),
key category_id (category_id),
key category_rank (category_rank)
)";
//===================================================================
//系统频道参数表
//===================================================================
$Asen_badmin_schema = "
create table Asen_badmin_table (
uid int(11) unsigned not null auto_increment,
board_name_code varchar(30),
board_total_access int(11) unsigned default '0' not null,
board_total_record int(11) unsigned default '0' not null,
board_skin varchar(255),
board_gohome varchar(255),
board_gohome_target varchar(20),
table_width int(5) default '95' not null,
str_length int(11) unsigned default '35' not null,
list_num tinyint(2) default '15' not null,
page_num tinyint(2) default '10' not null,
comment_num tinyint(2) default '15' not null,
browser_title varchar(255),
board_title varchar(255),
board_title_image varchar(255),
board_header_text text,
board_tailer_text text,
board_hlist_text text,
board_tlist_text text,
board_header_file varchar(255),
board_tailer_file varchar(255),
board_basic_text text,
board_basictop_text text,
board_comment_text text,
use_list char(1) default '1' not null,
use_category char(1) default '0' not null,
use_html char(1) default '0' not null,
use_filter char(1) default '1' not null,
use_link1 char(1) default '0' not null,
use_link2 char(1) default '0' not null,
use_file1 char(1) default '0' not null,
use_file2 char(1) default '0' not null,
file_size int(11) default '2097152' not null,
use_autolink char(1) default '1' not null,
use_comment char(1) default '1' not null,
use_secret char(1) default '0' not null,
use_search char(1) default '1' not null,
use_viewip char(1) default '0' not null,
use_spam char(1) default '0' not null,
use_print char(1) default '0' not null,
remote_connect char(1) default '0' not null,
ip_select char(1) default '0' not null,
filter_text text,
ip_close text,
ip_connect text,
name_close text,
use_mailer char(1) default '0' not null,
use_admin_remail char(1) default '0' not null,
use_white_remail char(1) default '0' not null,
class_list tinyint(2) default '0' not null,
class_view tinyint(2) default '0' not null,
class_write tinyint(2) default '0' not null,
class_comment tinyint(2) default '0' not null,
class_reply tinyint(2) default '0' not null,
class_modify tinyint(2) default '7' not null,
class_delete tinyint(2) default '7' not null,
class_notice tinyint(2) default '7' not null,
class_secret tinyint(2) default '7' not null,
class_html tinyint(2) default '0' not null,
class_download tinyint(2) default '0' not null,
class_print tinyint(2) default '0' not null,
class_approval tinyint(2) default '0' not null,
approval_date int(11) unsigned default '0' not null,
register_date int(11) unsigned default '0' not null,
primary key (uid)
)";
//===================================================================
//统计信息表
//===================================================================
$Asen_counter_schema = "
create table Asen_counter_table (
uid int(11) unsigned not null auto_increment,
counter_total int(11) unsigned default '0' not null,
counter_max int(11) unsigned default '0' not null,
counter_min int(11) unsigned default '0' not null,
counter_yesterday int(11) unsigned default '0' not null,
counter_now int(11) unsigned default '0' not null,
counter_regdate int(8) default '0' not null,
primary key (uid)
)";
//===================================================================
//短信表
//===================================================================
$Asen_info_schema = "
create table Asen_info (
`id` int(11) NOT NULL auto_increment,
`id_send` int(11) NOT NULL default '0',
`id_get` int(11) NOT NULL default '0',
`name_send` varchar(128) NOT NULL default '',
`name_get` varchar(128) NOT NULL default '',
`title` varchar(128) NOT NULL default '',
`content` text NOT NULL,
`time` datetime NOT NULL default '0000-00-00 00:00:00',
`orlook` tinyint(4) NOT NULL default '0',
`de_send` tinyint(4) NOT NULL default '0',
`de_get` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`id`)
)";
界面截图
我定义该数据库安装程序包含这几个步骤:
协议(Licence)
设置(Setting)
进度(Processing)
完成(Completed)
步骤窗体截图依次如下:
1、协议
2、设置
3、进度
4、完成
[ 本帖最后由 特蓝克斯 于 2007-1-5 14:37 编辑 ]
作者: 特蓝克斯 发布时间: 2007-01-05
安装文件
安装程序使用安装文件内的SQL脚本变量来执行数据库操作,SQL教本安装文件主要是创建数据库表记录信息,通过将这些脚本打包进行表结构创建。具体安装文件内容如下:
引用:
安装文件介绍:install.php 安装协议文件
install1.php 安装步骤1:数据库创建及登录用户信息文件
install2.php 安装步骤2:记录管理员信息文件
install_ok.php 安装步骤3:执行SQL脚本文件,并初始化部分数据信息。
schema.php SQL脚本结构文件
install2.php文件
代码:
<?include "includes/Fel_AsenFunction.php"; //包含处理文件
include "config.inc.php";
$db_host = $_POST['db_host'];
$db_id = $_POST['db_id'];
$db_pass = $_POST['db_pass'];
$db_name = $_POST['db_name'];
$db_create = $_POST['db_create'];
//以上5行均为获取表单信息并赋予相应变量。
if ($db_create==1){
$conn = @mysql_connect($db_host,$db_id,$db_pass) or error_msg("连接数据库时出错,请您正确输入。",""); //建立数据连接
if (!mysql_query("CREATE DATABASE $db_name",$conn))
{
echo "<title> 安装错误,创建的数据库以存在! </title>";
echo "<div style='font-size:12px;font-weight:bold;border:1px solid #006;padding:6px;background:#fcc' align='center'>Error Creating DataBase: ". mysql_error()." <a href='javascript:history.go(-1)' >返回上页</a></div>";
exit;
}// Create table in my_db database
}
else
{
$conn = @mysql_connect($db_host,$db_id,$db_pass) or error_msg("连接数据库时出错,请您正确输入。","");
}
if(file_exists("config_db.php")) error_msg("设置文件已经存在.\\n\\n若要重新安装,安装前请删除设置文件! .");
//
@mysql_select_db($db_name, $conn) or error_msg("连接数据库时出错,数据库名错误.","");
mysql_close ($conn); //释放数据连接
?>
代码:
<?/*
文件名称:install_ok.php
功 能:处理文件
作 者:特蓝克斯
建立时间:2006-12-5
注 释:接收schema.php文件中得表结构.
*/
$connect = @mysql_connect($db_host,$db_id,$db_pass) or die("<font size='2'> Mysql_Query : ".$query."<br> Mysql_Error : ".mysql_error()."<br>Mysql Error Num : ".mysql_errno()."</font>"); //or die --本函数将送出字符串到浏览器并中断 PHP 程序 (Script)。参数 message 为欲送出的字符串。
mysql_select_db($db_name, $connect) or die("<font size='2'> Mysql_Query : ".$query."<br> Mysql_Error : ".mysql_error()."<br>Mysql Error Num : ".mysql_errno()."</font>");
//自定义SQL查询语句处理。
function Asen_query_error($query) {
global $connect;
$result = mysql_query($query, $connect) or die("<font size='2'> Mysql_Query : ".$query."<br> Mysql_Error : ".mysql_error()."<br>Mysql Error Num : ".mysql_errno()."</font>");
return $result;
}
//判断安装第2部正常
if($install == "1") {
$re_result = mysql_list_tables($db_name);
for ($i = 0; $i < mysql_num_rows($re_result); $i++) {
if(ereg("Asen_",mysql_tablename($re_result, $i))) {
$del_query = "drop table ". mysql_tablename($re_result, $i);
Asen_query_error($del_query);
}
}
}
if($permit == "1") {
@chmod("database",0707); //-- 改变文件模式
@chmod("session",0707);
}
$g_board_header_text = "<div align=\"center\">";
$g_board_tailer_text = "</div>";
$g_database_dir = "database";
include "schema.php";
$now_time = time();
$pass1 = md5($adminpass);
//初始化数据表
Asen_query_error($Asen_User_schema);
$query = "insert into Asen_User_table set
User_class = '10',
User_id = '$adminid',
User_pass = '$pass1',
User_name = '管理员',
register_date = '$now_time',
approval_date = '$now_time'";
Asen_query_error($query);
Asen_query_error($Asen_category_schema);
$query = "insert into Asen_category_table set
category_id = '00000000',
category_depth = '0',
category_name = '分类编码',
category_rank = '0',
register_date = '$now_time'";
Asen_query_error($query);
Asen_query_error($Asen_counter_schema);
$query = "insert into Asen_counter_table set counter_total = '0'";
Asen_query_error($query);
Asen_query_error($Asen_info_schema);
Asen_query_error($Asen_badmin_schema);
$query = "insert Asen_badmin_table set
board_name_code = 'qna',
board_gohome = 'index.php',
board_gohome_target = '_blank',
board_skin = 'default',
browser_title = '客户疑难问题解答',
board_title = '客户疑难问题解答',
board_header_text = '$g_board_header_text',
board_tailer_text = '$g_board_tailer_text',
board_header_file = 'm_header.php',
board_tailer_file = 'm_bottom.php',
class_reply = '7',
approval_date = '$now_time',
register_date = '$now_time'";
Asen_query_error($query);
@mkdir("database/qna",0777);
$query = "insert Asen_badmin_table set
board_name_code = 'freeboard',
board_gohome = 'index.php',
board_gohome_target = '_blank',
board_skin = 'default',
browser_title = '客户及供货商留言',
board_title = '客户及供货商留言',
board_header_text = '$g_board_header_text',
board_tailer_text = '$g_board_tailer_text',
board_header_file = 'm_header.php',
board_tailer_file = 'm_bottom.php',
approval_date = '$now_time',
register_date = '$now_time'";
Asen_query_error($query);
@mkdir("database/freeboard",0777);
$query = "insert Asen_badmin_table set
board_name_code = 'Notice',
board_gohome = 'index.php',
board_gohome_target = '_blank',
board_skin = 'default',
browser_title = '网站公告',
board_title = '网站公告',
board_header_text = '$g_board_header_text',
board_tailer_text = '$g_board_tailer_text',
board_header_file = 'm_header.php',
board_tailer_file = 'm_bottom.php',
class_write = '7',
class_reply = '7',
approval_date = '$now_time',
register_date = '$now_time'";
Asen_query_error($query);
@mkdir("database/notice",0777);
mysql_close ($connect);
// 生成 config_db.php 文件信息
$config="<?
/*
###############################################################################
文件名称: config_db.php
功 能: 记录数据库信息
作 者: 特蓝克斯
建立时间: 2006-12-12
注 释: 数据库安装成功后即生成本文件。
###############################################################################
con_db_host = \"$db_host\"
con_db_id = \"$db_id\"
con_db_pass = \"$db_pass\"
con_db_name = \"$db_name\"
*/
?>";
$fp=fopen("config_db.php", w);
fputs($fp,$config);
fclose($fp);
error_msg("已经安装成功,请登陆.","./index.php?ps_install=ok");
?>
安装完成后,在Mysql创建我们刚才创建库及表结构信息,并在当前目录下创建一个config_db.php的配置记录文件。这样用PHP实现Mysql数据库安装程序就完成了,其实在配置的时候还有很多技巧,本文也只是抛砖引玉。
[ 本帖最后由 特蓝克斯 于 2007-1-5 14:36 编辑 ]
作者: 特蓝克斯 发布时间: 2007-01-05

作者: leehui1983 发布时间: 2007-01-05



作者: �����i�i 发布时间: 2007-01-07
作者: muqiao 发布时间: 2007-01-07
引用:
原帖由 leehui1983 于 2007-1-5 15:37 发表
作者: 特蓝克斯 发布时间: 2007-01-08
作者: coolsail 发布时间: 2007-01-09
作者: popoo 发布时间: 2007-01-09
作者: yyb 发布时间: 2007-03-07
作者: niohe 发布时间: 2007-03-08
作者: coolsail 发布时间: 2007-03-12
作者: sanler 发布时间: 2007-05-28
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28