维护MySQL数据库有时确实是个问题,我在不知道phpMyAdmin的时候自己做了一个页面,比较粗,安全性也不好,但确实可用,且简单。如果只是个人主页的话我觉得还是比较方便的。大家也可以根据自己的喜好随意修改。
页面会列出所有的数据表,点击表可以查看数据。在多行文本框里输入SQL语句既可执行并返回结果,但是必须输入自己指定的密码。
把 $dbman->SetDbVariables("host", "user", "pass", "db");这行中的参数改为自己的实际参数。
在if (isset ($strTN) || (isset ($strPass) && ($strPass == "xxx")))这行中把xxx改为自己设的密码,以后每次执行SQL的时候都必须填写密码框。
<?php
class ZDbMan
{
var $db;
var $strDbHost, $strDbUser, $strDbPass, $strDbName;
var $strQuery, $strErr;
var $strTableName;
var $nPage, $nPageSize;
var $strOutput;
function ZDbMan ()
{
$this->db = 0;
$this->strDbHost = "";
$this->strDbUser = "";
$this->strDbPass = "";
$this->strDbName = "";
$this->strErr = "";
$this->strTableName = "";
$this->nPage = 1;
$this->nPageSize = 20;
$this->strOutput = "";
}
function OpenDb ()
{
$this->db = mysql_connect ($this->strDbHost, $this->strDbUser, $this->strDbPass);
mysql_select_db ($this->strDbName);
}
function CloseDb ()
{
mysql_close ($this->db);
$this->db = 0;
}
function DoQuery ()
{
$nQueryResult = mysql_query ($this->strQuery, $this->db);
$err = mysql_error();
if ($err)
{
$this->strErr .= $err;
return;
}
$this->strOutput .= "<table border=1> ";
while ($aryRow = mysql_fetch_array ($nQueryResult))
{
$str = "<tr> ";
for ($i = 0; $i < count ($aryRow); $i ++)
{
$str .= "<td>" . $aryRow[$i] . "</td> ";
}
$str .= "</tr> ";
$this->strOutput .= $str;
}
$this->strOutput .= "</table> ";
}
function SetRecordList ()
{
global $PHP_SELF;
if (($this->db == 0) || ($this->strTableName == ""))
return;
$strQuery = "SELECT Count(*) AS Rec_Count FROM " . $this->strTableName;
$nQueryResult = mysql_query ($strQuery, $this->db);
if ($aryRow = mysql_fetch_array ($nQueryResult))
$nCount = $aryRow["Rec_Count"];
else
$nCount = 0;
$nFirstRecord = ($this->nPage - 1) * $this->nPageSize;
if ($nFirstRecord >= $nCount)
$nFirstRecord = $nCount - $this->nPageSize;
if ($nFirstRecord < 0)
$nFirstRecord = 0;
$nLastRecord = $nFirstRecord + $this->nPageSize;
if ($nLastRecord >= $nCount)
$nLastRecord = $nCount - 1;
if ($nFirstRecord > 0)
$this->strOutput .= " <a href=" . $PHP_SELF . "?strTN=" . $this->strTableName . "&nPage=" . ($this->nPage - 1) . ">PrevPage</a> ";
else
$this->strOutput .= " PrevPage ";
if ($nLastRecord < $nCount - 1)
$this->strOutput .= " <a href=" . $PHP_SELF . "?strTN=" . $this->strTableName . "&nPage=" . ($this->nPage + 1) . ">NextPage</a> ";
else
$this->strOutput .= " NextPage ";
$this->strOutput .= "<br> ";
$strQuery = "SELECT * FROM " . $this->strTableName . " LIMIT " . $nLastRecord;
$nQueryResult = mysql_query ($strQuery, $this->db);
$err = mysql_error();
if ($err)
{
$this->strErr .= $err;
return;
}
$nCurRecord = 0;
$this->strOutput .= "<table border=1> ";
while (($nCurRecord < $nFirstRecord) && ($aryRow = mysql_fetch_array ($nQueryResult)))
$nCurRecord ++;
while (($nCurRecord < $nLastRecord) && ($aryRow = mysql_fetch_array ($nQueryResult)))
{
$str = "<tr> ";
for ($i = 0; $i < count ($aryRow); $i ++)
{
$str .= "<td>" . $aryRow[$i] . "</td> ";
}
$str .= "</tr> ";
$this->strOutput .= $str;
$nCurRecord ++;
}
$this->strOutput .= "</table> ";
}
function SetTableList ()
{
global $PHP_SELF;
if ($this->db != 0)
{
$this->strOutput .= "<table> ";
$this->strOutput .= "<tr><td><font class=ftTitle>Tables</font></td></tr> ";
$nResult = mysql_list_tables ($this->strDbName);
$nTableCount = mysql_num_rows ($nResult);
for ($i = 0; $i < $nTableCount; $i++)
{
$strTableName = mysql_tablename ($nResult, $i);
$str = "<tr><td><a href=" . $PHP_SELF . "?strTN=" . $strTableName . ">";
$str .= $strTableName . "</a></td></tr> ";
$this->strOutput .= $str;
}
$this->strOutput .= "</table> ";
}
}
function SetOutput ()
{
$this->OpenDb ();
if ($this->strTableName != "")
$this->SetRecordList ();
if ($this->strQuery != "")
$this->DoQuery ();
$this->SetTableList ();
$this->CloseDb ();
}
function SetDbVariables ($strHost, $strUser, $strPass, $strDb)
{
$this->strDbHost = $strHost;
$this->strDbUser = $strUser;
$this->strDbPass = $strPass;
$this->strDbName = $strDb;
}
function SetQueryVariables ($strQuery)
{
$this->strQuery = $strQuery;
$this->strTableName = "";
}
function SetTableVariables ($strName, $nPage)
{
$this->strTableName = $strName;
$this->nPage = $nPage;
$strQuery = "";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Db Manager</title>
</head>
<style type="text/css">
<!--
.font {font-size: 9pt}
.ftTitle {font-size:12pt; color:#510051}
-->
</style>
<body>
<?php
$dbman = new ZDbMan;
$dbman->SetDbVariables ("host", "user", "pass", "db");
if (isset ($strTN))
{
if (!isset ($nPage))
$nPage = 1;
$dbman->SetTableVariables ($strTN, $nPage);
} else
{
if (isset ($strQuery))
$dbman->SetQueryVariables ($strQuery);
}
if (isset ($strTN) || (isset ($strPass) && ($strPass == "xxx")))
{
$dbman->SetOutput ();
printf ($dbman->strOutput);
}
?>
<hr>
<form action=<?php printf ($PHP_SELF) ?> method=post>
<table>
<tr><td>Query</td>
<td><textarea name=strQuery rows=4 cols=64></textarea>
</td>
</tr>
<tr><td>Password</td>
<td><input type=password name=strPass></td>
</tr>
<tr><td><input type=submit value=submit></td>
<td><input type=reset value=reset>
</tr>
</table>
</form>
</body>
</html>
|