程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 用浏覽器遠程創建Mysql數據庫的簡易程序

用浏覽器遠程創建Mysql數據庫的簡易程序

編輯:MySQL綜合教程

近日來學習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便可.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved