代码拷贝框
/********************************************
* *
* Name : MySQL Manager *
* Author : Windy_sk *
* Email : flyhorses@sina.com *
* 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
$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
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
@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()."
\n";
$result .= "MySQL protocol version: ".mysql_get_proto_info()."
\n";
$result .= "MySQL host info: ".mysql_get_host_info()."
\n";
$result .= "MySQL client info: ".mysql_get_client_info()."
\n";
$result .= str_replace(" ","
\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
$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
$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
$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
$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 "
".str_replace("\n", "\n
\n", $err_msg);
$this->Close();
die();
}
}
?>
[Ctrl+A 全部选择 然后拷贝]