当然还有很多的缺点,比如非关键中止,俺也让程序退出了,但退出前没有善后。
还有一些SQL格式需要申明,比如SQL文件含有空行的处理
这里先不以;为处理,以一行为处理操作起来方便一点。但这个是严重的BUG。
流程图
install.php安装文件
PHP代码
<?php
//============================
// FileName: install.php
// Version: 0.0.1
// Author: Leo
// Date C/M: 2007-06-08
// Content: PHP install MySQL
//============================
function ErrorInfo()
{
return "<ul style='font-family:Courier;font-size:11px;background:#FDF5E6;color:#696969;margin:3px;padding:10px;border:1px solid #696969;'>Notice!: System Error<li style='font-family:Courier;list-style-type:none'>ErrInfo: ".mysql_error()."</li><li style='font-family:Courier;list-style-type:none'>ErrCode: ".mysql_errno()."</li><li style='font-family:Courier;list-style-type:none'>ErrURIs: ".$_SERVER['REQUEST_URI']."</li></ul>";
}
if(isset($_POST['install'])) //提交创建
{
$dbserver=$_POST['dbhost'].(isset($_POST['dbport'])?(":".$_POST['dbport']):'');
$conn=@mysql_connect($dbserver,$_POST['dbuser'],$_POST['dbpass']) or die(ErrorInfo()); //连接到MySQL Server
if($conn) //成功
{
if(isset($_POST['dropold']))@mysql_query("DROP DATABASE IF EXISTS `".$_POST['dbname']."` ;") or die(ErrorInfo()); //按条件删除原DB
echo "<br />Creating Database ...";
@mysql_query("CREATE DATABASE IF NOT EXISTS `".$_POST['dbname']."` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;") or die(ErrorInfo()); //创建DB
echo "OK!";
$linktodb=@mysql_select_db($_POST['dbname'],$conn) or die(ErrorInfo()); //打开数据库
echo "<br />Creating TABLEs ";
if($ftable=fopen('table.sql','r')) //创建表
{
while (!feof($ftable))
{
@mysql_query(fgets($ftable)) or die(ErrorInfo());
}
echo "OK!";
fclose($ftable);
}
else //不能创建表或者创建有误
{
echo "Failed!";
echo "<br />Droping Database ...";
@mysql_query("DROP DATABASE IF EXISTS ".$_POST['dbname']." ;") or die(ErrorInfo()); //删除所创建DB
echo "OK!";
exit();
}
//把信息写入config.inc.php
if($fp=fopen("config.inc.php",'x'))
{
$configstr="<?php\n//数据库信息\ndefine('_DBHOST','".$dbserver."');\t\t//数据库服务器\ndefine('_DBUSER','".$_POST['dbuser']."');\t\t\t\t//数据库用户\ndefine('_DBPASS','".$_POST['dbpass']."');\t //数据库用户密码\ndefine('_DBNAME','".$_POST['dbname']."');\t\t\t//数据库名称\n//连接到数据库\n\$dblink=@mysql_connect(_DBHOST,_DBUSER,_DBPASS) or die(ErrorInfo()); //数据库连接号\n\$dbnow=@mysql_select_db(_DBNAME,\$dblink) or die(ErrorInfo());\t\t//选择当前数据库\n?>";
echo "<br />Writing <strong>config.inc.php</strong> ";
if(fwrite($fp,$configstr))
{
echo "OK!";
fclose($fp);
}
else
{
echo "Failed!";
fclose($fp);
exit();
}
}
else
{
echo "<br /><strong>Make config File Failed</strong> : you must manual Modify config file:<strong>config.inc.php</strong>!";
exit();
}
if(isset($_POST['demo']))
{
echo "<br />Creating DEMO data ";
if($fdemo=fopen('demo.sql','rb')) //装载演示数据
{
while (!feof($fdemo))
{
@mysql_query(fgets($fdemo)) or die(ErrorInfo());
}
echo "OK!";
fclose($fdemo);
}
else //装载失败
{
echo "Failed!";
exit();
}
}
echo "<br />LOCK install.php ";
if(rename('install.php','install.php.lock'))
{
echo "OK!";
}
else
{
echo "Failed!";
}
echo "<br />Install Succes! <a href='disp.php'> Disp the DEMO</a>";
}
else
{
echo "<br /><strong>Install interrupt</strong> : Some data you input is not fit OR DATABASE server have an Error!";
exit();
}
}
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transititonal.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn" lang="zh-cn" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="AJAX,HENT studios,PHP,MySQL" />
<style>
<!--
#db{width:300px;background:#FAEBD7;padding:10px;}
p{margin:1px;padding:4px;font-family:Arial;border:1px solid #DCDCDC;}
-->
</style>
<title>Installer DEMO - HENT Studios</title>
<script language="javascript">
<!--
function chkform(oForm)
{
return true;
}
-->
</script>
</head>
<body>
<h3>MySQL的PHP安装脚本</h3>
<div id="db">
<form action="<?php echo $_SERVER['REQUEST_URI'];?>" method="post" onsubmit="return chkform(this)">
<p>数据库的服务器: <input type="text" name="dbhost" id="dbhost" value="localhost" /> *</p>
<p>数据库通信端口: <input type="text" name="dbport" id="dbport" /></p>
<p>数据库用户名称: <input type="text" name="dbuser" id="dbuser" value="root" /> *</p>
<p>数据库用户密码: <input type="password" name="dbpass" id="dbpass" /> *</p>
<p>[以上数据请从数据库管理员处获取]</p>
<p>需创建的数据库: <input type="text" name="dbname" id="dbname" value="yourdb"/></p>
<p><input type="checkbox" name="dropold" id="dropold" />删除同名旧数据库(不可恢复!)</p>
<p><input type="checkbox" name="demo" id="demo" checked="chkecked" />加载演示数据</p>
<p><input type="submit" name="install" id="install" value="开始安装" /> <input type="reset" value="清除重写" /></p>
</form>
</div>
</body>
</html>
<?php
}
?>
creattable.sql
SQL代码
DROP TABLE IF EXISTS `testtab`;
CREATE TABLE IF NOT EXISTS `testtab` (`tid` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`tdate` DATETIME NOT NULL ,`ttext` VARCHAR( 255 ) NOT NULL ) ENGINE = MYISAM COMMENT = '七月十五的测试表';
demo.sql
SQL代码
INSERT INTO testtab VALUES (1,'2007-06-08 21:42:11','十五:我的第一个测试数据,不知道UTF-8会不会乱码哦:)');
INSERT INTO testtab VALUES (2,'2007-06-08 22:43:10','伤感:听说七月十五要做中国第一WEB设计师');
INSERT INTO testtab VALUES (3,'2007-06-08 23:44:09','一抹:这家伙就会扯!');
INSERT INTO testtab VALUES (4,'2007-06-09 00:45:08','宝贝:他要是不懒,还真说不定能成……,可是他会不懒么?');
INSERT INTO testtab VALUES (5,'2007-06-10 01:46:07','一客:他要是不懒,所有的猪都上树了');
disp.php
PHP代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transititonal.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn" lang="zh-cn" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="AJAX,HENT studios,PHP,MySQL" />
<style>
<!--
#db{width:600px;background:#FAEBD7;padding:10px;}
table{font-size:14px;}
th,td{border:1px solid #DCDCDC;}
p{margin:1px;padding:4px;font-family:Arial;border:1px solid #DCDCDC;}
-->
</style>
<title>Installer DEMO - HENT Studios</title>
<script language="javascript">
<!--
function chkform(oForm)
{
return true;
}
-->
</script>
</head>
<body>
<h3>列出演示数据</h3>
<div id="db">
<?php
//============================
// FileName: disp.php
// Version: 0.0.1
// Author: Leo
// Date C/M: 2007-06-08
// Content: 列出演示数据
//============================
require_once("config.inc.php");
if(!mysql_num_rows($re=mysql_query("SELECT * FROM testtab ORDER BY tdate DESC ")))
{
echo "<p>没有数据</p>";
}
else
{
while($rs=mysql_fetch_array($re))
{
$str.="<tr><td>".$rs['tid']."</td><td>".$rs['tdate']."</td><td>".$rs['ttext']."</td></tr>";
}
echo "<table><thead><tr><th>序号</th><th>发布时间</th><th>内容</th></tr></thead><tbody>".$str;
}
?>
</tbody></table>
</div>
</body>
</html>
评论