热烈祝贺台州朗动科技的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » MySQL/MSSQL编程 » PHP源代码集锦-MySQL Manager

PHP源代码集锦-MySQL Manager

论坛链接
  • PHP源代码集锦-MySQL Manager
  • 发布时间:2007-09-26 10:37:20    浏览数:12987    发布者:sunhw0725    设置字体【   
<?php
/********************************************
* *
* Name : MySQL Manager *
* Author : Windy_sk *
* Email : [email]flyhorses@sina.com[/email] *
* HomePage: None (Maybe Soon) *
* Notice : U Can Use & Modify it freely, *
* BUT PLEASE HOLD THIS ITEM. *
* *
********************************************/

/*--------------------------------------------------------------------------------------------------------------------

How To Use:
$MySQL = new MySQL($host, $user, $pass) // Set the Database Class
$MySQL->ChangUser($user, $pass, $db="") // Change the Database User (Unusable in some versoin of MySQL)
$MySQL->OptimizeTab() // Optimize the Tablses of Selected Database
$MySQL->ReadSqlFile($file) // Read SQL File And Send Content of the File to HandleSQL($strSQL)
$MySQL->HandleSQL($strSQL) // Split the SQL Query String into a array from a whole String (Maybe Read from a File)
$MySQL->BatchExec($ArrSQL) // Execute Multi Query from an Array (Use HandleSQL First)
$MySQL->Connect($pconnect = false) // Build a Connection to MySQL to $MySQL->DB_conn
$MySQL->SelectDB($the_db) // Select a Database of MySQL to $MySQL->DB_select (Must Build Connect First)
$MySQL->Query($sql) // Execute a Query of MySQL, Result into $MySQL->DB_resut
$MySQL->SeekData($line) // Seek Data Row in the $MySQL->DB_resut ($MySQL->DB_Qtype Must Be Setted into True Before Query)
$MySQL->GetResult($line, $field="") // The Same Use as mysql_result ($MySQL->DB_Qtype Must Be Setted into True Before Query)
$MySQL->GetRS() // Return The Current Result as an Array and Set the Point of Result to the Next Result
$MySQL->GetStat() // Get the Current Status of MySQL
$MySQL->GetDBs() // Get the Databases List of Current MySQL Server as an Array
$MySQL->GetTabs($the_db) // Get the Tables List of Current Selected Database as an Array
$MySQL->GetIdx($the_tab) // Get the Indexes List of a Table as an Array
$MySQL->GetTabSetting($the_tab) // Get the Whole Struction of Current Selected Database as an Array
$MySQL->GetTabData($the_tab) // Get All of The Data of a Table
$MySQL->GetTabFields($the_db, $the_tab) // Get the Columns List of a Table as an Array
$MySQL->GetQueryFields() // Get the Columns List of Current Query
$MySQL->Free() // Free the $MySQL->DB_result in order to Release the System Resource
$MySQL->Close() // Close Current MySQL Link
$MySQL->Error($str) // Handle the Errors

--------------------------------------------------------------------------------------------------------------------*/

class MySQL {
var $DB_host = "";
var $DB_user = "";
var $DB_pass = "";
var $DB_db = "";
var $DB_conn = NULL;
var $DB_select = NULL;
var $DB_error = false;
var $DB_qstr = "";
var $DB_result = NULL;
var $DB_RStype = 1;
var $DB_Qtype = false;


function MySQL($host, $user, $pass) {
$this->DB_serv = $host;
$this->DB_user = $user;
$this->DB_pass = $pass;
return;
}


function ChangUser($user, $pass, $db="") { // Maybe doesn't work !
eval("\$result = mysql_change_user('$user', '$pass'".($db==""?"":", '$db'").");");
return $result;
}


function OptimizeTab() {
if($this->DB_select == NULL || $this->DB_conn == NULL) return false;
$tabs = $this->GetTabs($this->DB_db);
for($i=0; $i<count($tabs); $i++) {
$this->Query("OPTIMIZE TABLE ".$tabs[$i]);
}
$this->Free();
return true;
}


function ReadSqlFile($file) {
return is_file($file)?$this->HandleSQL(join("",file($file))):"";
}


function HandleSQL($strSQL) {
$strSQL = trim($strSQL);
$strSQL = preg_replace("/^#[^\n]*\n?$/m", "", $strSQL);
$strSQL = preg_replace("/\r\n/", "\n", $strSQL);
$strSQL = preg_replace("/[\n]+/", "\n", $strSQL);
$strSQL = preg_replace("/[\t ]+/", " ", $strSQL);
$strSQL = preg_replace("/\/\*[^(\*\/)]*\*\//", "", $strSQL);
$temp = split(";",$strSQL);
$result = array();
for($i=0; $i<count($temp); $i++) {
if(str_replace("\n","",$temp[$i]) != "") {
$result[] = preg_replace("/^\n*(.*)\n*$/m","\\1",$temp[$i]);
}
}
return($result);
}


function BatchExec($ArrSQL){
if($this->DB_select == NULL || $this->DB_conn == NULL) return false;
for($i=0; $i<count($ArrSQL); $i++) {
@mysql_unbuffered_query($ArrSQL[$i], $this->DB_conn);
$this->DB_qstr = $ArrSQL[$i];
if(mysql_error() != "") $this->Error("Error Occur in Batch Query");
}
return true;
}


function Connect($pconnect = false) {
if($pconnect) {
$this->DB_conn = @mysql_pconnect($this->DB_host, $this->DB_user, $this->DB_pass);
} else {
$this->DB_conn = @mysql_connect($this->DB_host, $this->DB_user, $this->DB_pass);
}
$this->DB_qstr = "none (Connect to MySQL Server)";
if(mysql_error() != "") $this->Error("Could not connect to MySQL Server");
return;
}


function SelectDB($the_db) {
if($this->DB_conn == NULL) return false;
$this->DB_db = $the_db;
$this->DB_select = @mysql_select_db($the_db, $this->DB_conn);
$this->DB_qstr = "none (Select Database)";
if(mysql_error() != "") $this->Error("Could not connect to the Database");
return true;
}


function Query($sql) {
if($this->DB_conn == NULL) return false;
$this->Free();
$ifsel = strstr("|selec|show |descr|expla|", strtolower(substr(trim($sql), 0, 5)));
if($this->DB_Qtype) {
$this->DB_result = @mysql_query($sql, $this->DB_conn);
} else {
$this->DB_result = @mysql_unbuffered_query($sql, $this->DB_conn);
}
$this->DB_qstr = $sql;
if($ifsel) {
$num_rows = @mysql_num_rows($this->DB_result);
} else {
$num_rows = @mysql_affected_rows($this->DB_conn);
$this->Free();
}
if(mysql_error() != "") $this->Error("Error Occur in Query !");
return $num_rows;
}


function SeekData($line) {
if(!$this->DB_Qtype || $this->DB_result == NULL) return false;
$flag = @mysql_data_seek($this->DB_result, $line);
if(mysql_error() != "") $this->Error("Error Occur in Query !");
return $flag;
}


function GetResult($line, $field=""){
if(!$this->DB_Qtype || $this->DB_result == NULL) return false;
eval("\$result = @mysql_result(\$this->DB_result, $line".(empty($field)?"":", '$field'").");");
if(mysql_error() != "") $this->Error("Error Occur in Query !");
return $result;
}


function GetRS(){
if($this->DB_result == NULL) return false;
switch($this->DB_RStype){
case 1:
$flag = ($row = @mysql_fetch_assoc($this->DB_result));
break;
case 2:
$flag = ($row = @mysql_fetch_row($this->DB_result));
break;
case 3:
$flag = ($row = @mysql_fetch_array($this->DB_result));
break;
default:
$flag = ($row = @mysql_fetch_assoc($this->DB_result));
}
$this->DB_qstr = "none(Get Recordset)";
if(mysql_error() != "") $this->Error("Error Occur in Get Recordset !");
return ($flag?$row:false);
}


function GetStat() {
if($this->DB_conn == NULL) return "";
$result = "";
$result .= "MySQL server version: ".mysql_get_server_info()."<br />\n";
$result .= "MySQL protocol version: ".mysql_get_proto_info()."<br />\n";
$result .= "MySQL host info: ".mysql_get_host_info()."<br />\n";
$result .= "MySQL client info: ".mysql_get_client_info()."<br />\n";
$result .= str_replace(" ","<br /> \n",mysql_stat($this->DB_conn));
return $result;
}


function GetDBs() {
$this->Free();
$dbs = array();
$this->DB_result = @mysql_list_dbs($this->DB_conn);
$this->DB_qstr = "none (List Databases)";
if (mysql_error() != "") $this->Error("Could not List Databases");
$dbs = array();
$num_dbs = mysql_num_rows($this->DB_result);
for ($i = 0; $i < $num_dbs; $i++) {
$dbs[] = mysql_dbname($this->DB_result, $i);
}
$this->Free();
return $dbs;
}


function GetTabs($the_db) {
$this->Free();
$this->DB_result = @mysql_list_tables($the_db);
$this->DB_qstr = "none (List Tables of $the_db)";
if (mysql_error() != "") $this->Error("Could not List Tables");
$tabs = array();
$num_tabs = mysql_num_rows($this->DB_result);
for ($i = 0; $i < $num_tabs; $i++) {
$tabs[] = mysql_tablename($this->DB_result, $i);
}
$this->Free();
return $tabs;
}


function GetIdx($the_tab) {
$this->Free();
$this->DB_qstr = "SHOW INDEX FROM $the_tab";
$this->DB_result = mysql_query($this->DB_qstr);
if (mysql_error() != "") $this->Error("Could not List Table's Setting");
$idxes = array();
while($row = mysql_fetch_array($this->DB_result)){
if($row["Key_name"] != "PRIMARY") {
$tmp = $row["Column_name"];
if($row["Sub_part"] != "") $tmp .= "(".$row["Sub_part"].")";
if($row["Seq_in_index"] == 1) {
if(count($idxes) != 0)
$idxes[count($idxes)-1] .= ")";
$idxes[] = "INDEX ".$row["Key_name"]." (".$tmp;
} else {
$idxes[count($idxes)-1] .= ", $tmp";
}
}
}
if(count($idxes) != 0)
$idxes[count($idxes)-1] .= ")";
$this->Free();
return $idxes;
}


function GetPri($the_tab) {
$this->Free();
$this->DB_qstr = "SHOW FIELDS FROM $the_tab";
$this->DB_result = mysql_query($this->DB_qstr);
if (mysql_error() != "") $this->Error("Could not List Table's Setting");
$keys = "";
while($row = mysql_fetch_assoc($this->DB_result)) {
if($row["Key"] == "PRI" || $row["Key"] == "MUL")
$keys[] = $row["Field"];
}
$this->Free();
return $keys;
}


function GetTabSetting($the_tab) {
$this->Free();
$this->DB_qstr = "SHOW FIELDS FROM $the_tab";
$this->DB_result = mysql_query($this->DB_qstr);
if (mysql_error() != "") $this->Error("Could not List Table's Setting");
$result = "CREATE TABLE $the_tab (\n";
$P_key = "";
while($row = mysql_fetch_assoc($this->DB_result)) {
$string = " ".$row["Field"]." ".$row["Type"]." ";
if($row["Null"] == "") $string .= "NOT NULL ";
if($row["Default"] != "") $string .= "Default \"".$row["Default"]."\" ";
if($row["Key"] == "PRI") $P_key .= " PRIMARY KEY (".$row["Field"].") ,\n";
elseif($row["Key"] == "MUL") $P_key .= " KEY ".$row["Field"]." (".$row["Field"].") ,\n";
$string .= $row["Extra"].",\n";
$result .= $string;
}
$result .= $P_key;
$idxes = $this->GetIdx($the_tab);
for($i=0; $i<count($idxes); $i++) {
$result .= " ".$idxes[$i]." ,\n";
}
$result .= ");\n";
$this->Free();
return $result;
}


function GetTabData($the_tab) {
$this->Free();
$this->DB_qstr = "SELECT * FROM $the_tab";
$this->DB_result = mysql_query($this->DB_qstr);
if (mysql_error() != "") $this->Error("Could not List Table's Setting");
$result = "";
while($row = mysql_fetch_row($this->DB_result)) {
$result .= "INSERT INTO $the_tab VALUES (";
for($i=0; $i<count($row); $i++)
$result .= "\"".addslashes($row[$i])."\",";
$result .= ");\n";

}
$result = str_replace("\",);","\");",$result);
return $result;
}


function GetTabFields($the_db, $the_tab) {
$this->Free();
$this->DB_result = @mysql_list_fields($the_db, $the_tab, $this->DB_conn);
$this->DB_qstr = "none (List Fields of $the_tab)";
if (mysql_error() != "") $this->Error("Could not List Fields");
$fields = array();
$columns = mysql_num_fields($this->DB_result);
for ($i = 0; $i<$columns; $i++) {
$fields[] = mysql_field_name($this->DB_result, $i);
}
$this->Free();
return $fields;
}


function GetQueryFields() {
if($this->DB_result == NULL) return false;
$fields = array();
$columns = mysql_num_fields($this->DB_result);
for ($i = 0; $i<$columns; $i++) {
$fields[] = mysql_field_name($this->DB_result, $i);
}
return $fields;
}


function Free() {
@mysql_free_result($this->DB_result);
$this->DB_result = NULL;
return;
}


function Close() {
if($this->DB_result != NULL)
$this->Free();
if($this->DB_conn != NULL)
@mysql_close($this->DB_conn);
if($this->DB_select != NULL)
$this->DB_select = NULL;
return;
}


function Error($str) {
$err_msg = "";
$err_msg .= "Time: ".gmdate("Y-n-j G:i", time() + 8 * 3600)."\n";
$err_msg .= "File: ".$_SERVER["PHP_SELF"]."\n";
$err_msg .= "Error Message: $str \n";
$err_msg .= "Query String: ".$this->DB_qstr."\n";
$err_msg .= "MySQL Message: ".mysql_errno()." - ".mysql_error();
echo "<br />".str_replace("\n", "\n<br /><br />\n", $err_msg);
$this->Close();
die();
}
}
?>
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理