程序员人生 网站导航

用浏览器远程创建Mysql数据库的简易程序

栏目:MySql时间:2014-03-02 10:33:11

近日来学习mysql数据库,自己编写一个用ie远程创建mysql数据库的简易程序,此程序适用与对mysql数据库一无所知的人,可以轻松的创建mysql数据库及数据库中的各种类型的表,而且完全是图形界面,你甚至可以一句sql语句都不知道,我编写的这个程序可能尚还十分简陋,在此我只想起一个抛砖引玉的作用,希望大家能不断完善此程序,做一个真正意义上的图形界面的mysql管理器.
此程序共分两个文件:
给下面的.php文件使用.

<html>
<head>
<title>mysql数据库创建单</title>
</head>
<body>
<form method="post" action="mkdb.php">
<table width=400 border=1 cellspacing=0 cellpadding=0>
<tr>
<td align=center colspan=2>创建一个新的数据库</td>
</tr>
<tr>
<td width=150 align=center>请输入数据库名:</td>
<td width=250 align=center>
<input type=test size=30 value=database name=databasename>
</td>
</tr>
<tr>
<td align=center colspan=2>
<input type=submit value=创建数据库 name=swcode>
</td>
</tr>
</table>
</form>
<form method="post" action="mkdb.php">
<table width=400 border=1 cellspacing=0 cellpadding=0>
<tr>
<td align=center colspan=2>删除一个已有的数据库</td>
</tr>
<tr>
<td width=150 align=center>请输入数据库名:</td>
<td width=250 align=center>
<input type=test size=30 value=database name=databasename>
</td>
</tr>
<tr>
<td align=center colspan=2>
<input type=submit value=删除数据库 name=swcode>
</td>
</tr>
</table>
</form>
<form method="post" action="mkdb.php">
<table width=500 border=1 cellspacing=0 cellpadding=0>
<tr>
<td align=center colspan=2>在一个已有的数据库中创建表</td>
</tr>
<tr>
<td width=150 align=center>请输入数据库名:</td>
<td width=350>
<input type=test size=30 value=database name=databasename>
</td>
</tr>
<tr>
<td width=150 align=center>请输入表名:</td>
<td width=350>
<input type=test size=30 value=testable name=tablename>
</td>
</tr>
<tr>
<td align=center colspan=2>
<table width=500 border=1 cellspacing=0 cellpadding=0>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 value=idx name=fieldname[1]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[1] size=1>
<option value=integer(10) selected>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
</select>
</td>
</tr>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 name=fieldname[2]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[2] size=1>
<option value=integer(10)>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
<option value=none selected>空字段</option>
</select>
</td>
</tr>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 name=fieldname[3]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[3] size=1>
<option value=integer(10)>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
<option value=none selected>空字段</option>
</select>
</td>
</tr>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 name=fieldname[4]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[4] size=1>
<option value=integer(10)>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
<option value=none selected>空字段</option>
</select>
</td>
</tr>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 name=fieldname[5]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[5] size=1>
<option value=integer(10)>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
<option value=none selected>空字段</option>
</select>
</td>
</tr>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 name=fieldname[6]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[6] size=1>
<option value=integer(10)>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
<option value=none selected>空字段</option>
</select>
</td>
</tr>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 name=fieldname[7]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[7] size=1>
<option value=integer(10)>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
<option value=none selected>空字段</option>
</select>
</td>
</tr>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 name=fieldname[8]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[8] size=1>
<option value=integer(10)>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
<option value=none selected>空字段</option>
</select>
</td>
</tr>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 name=fieldname[9]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[9] size=1>
<option value=integer(10)>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
<option value=none selected>空字段</option>
</select>
</td>
</tr>
<tr>
<td width=75 align=center>字段名</td>
<td width=175 align=center><input type=test size=20 name=fieldname[10]></td>
<td width=75 align=center>字段类型</td>
<td width=175 align=center>
<select name=type[10] size=1>
<option value=integer(10)>普通整型</option>
<option value=tinyint(3)>短整型</option>
<option value=float(10,4)>单精度浮点型</option>
<option value=double(10,4)>双精度浮点型</option>
<option value=date>日期型</option>
<option value=varchar(255)>可变字符型</option>
<option value=char(20)>字符型(20个字符)</option>
<option value=none selected>空字段</option>
</select>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align=center colspan=2>
<input type=submit value=创建表 name=swcode>
</td>
</tr>
</table>
</form>
<form method="post" action="mkdb.php">
<table width=400 border=1 cellspacing=0 cellpadding=0>
<tr>
<td align=center colspan=2>删除一张已有的表</td>
</tr>
<tr>
<td width=150 align=center>请输入数据库名:</td>
<td width=250 align=center>
<input type=test size=30 value=database name=databasename>
</td>
</tr>
<tr>
<td width=150 align=center>请输入表格名称:</td>
<td width=250 align=center>
<input type=test size=30 value=testable name=tablename>
</td>
</tr>
<tr>
<td align=center colspan=2>
<input type=submit value=删除表 name=swcode>
</td>
</tr>
</table>
</form>
</body>
</html>



mkdb.php:通过函数联系mysql.

<html>
<head>
<title>mysql数据库创建结果</title>
<?php
function showdb($result)
{
echo "<center><table border=1 width=350 cellspacing=0 cellpadding=0>
<tr>
<td width=100 align=center>数据库序号</td>
<td width=250 align=center>数据库名</td>
</tr>";$i=0;
while($row=mysql_fetch_array($result))
{
$i=$i+1;
echo "<tr><td width=100 align=center>$i</td>";
echo "<td width=250 align=center>";
echo $row["database"];
echo "</td></tr></center>";
}
}
function showcol($result)
{
echo "<center><table border=1 width=350 cellspacing=0 cellpadding=0>
<tr>
<td width=100 align=center>字段名</td>
<td width=250 align=center>字段类型</td>
</tr>";$i=0;
while($row=mysql_fetch_array($result))
{
echo "<tr><td width=100 align=center>";
echo $row["field"];
echo "</td>";
echo "<td width=250 align=center>";
echo $row["type"];
echo "</td></tr></center>";
}
}
function showtab($result,$databasename)
{
echo "<center><table border=1 width=350 cellspacing=0 cellpadding=0>
<tr>
<td width=100 align=center>表格序号</td>
<td width=250 align=center>表格名称</td>
</tr>";$i=0;
while($row=mysql_fetch_array($result))
{
$i=$i+1;
echo "<tr><td width=100 align=center>$i</td>";
echo "<td width=250 align=center>";
echo $row["tables_in_".$databasename];
echo "</td></tr></center>";
}
}
function creatdb($dbname)
{
$result=mysql_create_db($dbname);
if ($result){
echo "<center><table border=0 width=350 cellspacing=0 cellpadding=0><tr><td align=center>";
echo "<h4>$dbname 创建成功!</h4>";
echo "</td></tr></table></center>";}
else{
echo "<center><table border=0 width=350 cellspacing=0 cellpadding=0><tr><td align=center>";
echo "<h4>创建失败!</h4>";
echo "</td></tr></table></center>";}
}
function dropdb($dbname)
{
$result=mysql_drop_db($dbname);
if ($result){
echo "<center><table border=0 width=350 cellspacing=0 cellpadding=0><tr><td align=center>";
echo "<h4>$dbname 成功删除!</h4>";
echo "</td></tr></table></center>";}
else{
echo "<center><table border=0 width=350 cellspacing=0 cellpadding=0><tr><td align=center>";
echo "<h4>删除失败!</h4>";
echo "</td></tr></table></center>";}
}
?>
</head>
<body>
<center><h3>数据库创建结果如下:</h3></center>
<?php
$result=mysql_connect();
switch($swcode){
case '创建数据库':
creatdb($databasename);
echo "<center><h4>目前数据库列表如下:</h4></br></center>";
$result=mysql_list_dbs();
showdb($result);
break;
case '删除数据库':
dropdb($databasename);
echo "<center><h4>目前数据库列表如下:</h4></br></center>";
$result=mysql_list_dbs();
showdb($result);
break;
case '创建表':
if (mysql_select_db($databasename))
{
if (mysql_db_query($databasename,"select * from $tablename"))
echo "<h4><center>在数据库: $databasename 中已存在 $tablename 表。请检查表名称!</center></h4>";
else
{
$string="create table ".$tablename."(";
if ($type[1]<>"none") $string=$string.$fieldname[1]." ".$type[1];
for($i=2;$i<=10;$i++)
{
if ($type[$i]<>"none") $string=$string.",".$fieldname[$i]." ".$type[$i];
}
$string=$string.")";
$result=mysql_db_query($databasename,"$string");
if ($result)
{
echo "<h4><center>成功的在数据库: $databasename 中创建 $tablename 表!</center></h4>";
$result=mysql_db_query($databasename,"show columns from $tablename");
showcol($result);
}
else
{
echo "<h4><center>在数据库: $databasename 中创建 $tablename 表的动作失败!请检查原因!</center></h4>";
}
}
}
else
echo "<center><h4>此数据库不存在!请检查数据库名。</h4></br></center>";
$result=mysql_db_query($databasename,"show tables");
if ($result)
{
echo "<center><h5>在".$databasename."中已存在的表格如下所示:</h5></center>";
showtab($result,$databasename);
}
break;
case '删除表':
if (mysql_select_db($databasename))
{
$result=mysql_db_query($databasename,"drop table $tablename");
if ($result)
{
echo "<center><h3>已成功的在数据库".$databasename."中将表".$tablename."删除</center></h3>";
$result=mysql_db_query($databasename,"show tables");  
if ($result)
{
echo "<center><h5>在".$databasename."中已存在的表格如下所示:</h5></center>";
showtab($result,$databasename);
}
}
else
{
echo "<center><h3>在数据库".$databasename."中未将表".$tablename."删除!</center></h3>";
$result=mysql_db_query($databasename,"show tables");  
if ($result)
{
echo "<center><h5>在".$databasename."中已存在的表格如下所示:</h5></center>";
showtab($result,$databasename);
}
}
}
else
echo "<center><h3>数据库".$databasename."不存在!</h3></center>";
break;
default:
echo "<center><h2>你什么事都没干!</h2></center>";
break;
}
?>
</body>
</html>


使用时,需将着两个文件分别放到apache的访问目录下,并用ie访问mkdb.html便可.

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐