Welcome Guest [Log In] [Register]
Add Reply
Optimise your Database
Topic Started: Mar 6 2009, 02:32 PM (202 Views)
php-coder
Member Avatar

Use this to optimise your database can be improved but it works for now to clear things up.


Code:
 

<?php

class mysql_db_optimize{

var $_dblink;

function Getdblink(){
return $this->_dblink;
} // function

function Setdblink($value){
$this->_dblink = $value;
} // function

var $_mysqlhost;

function Getmysqlhost(){
return $this->_mysqlhost;
} // function

function Setmysqlhost($value){
$this->_mysqlhost = $value;
} // function

var $_mysql_user;

function Getmysql_user(){
return $this->_mysql_user;
} // function

function Setmysql_user($value){
$this->_mysql_user = $value;
} // function

var $_mysql_password;

function Getmysql_password(){
return $this->_mysql_password;
} // function

function Setmysql_password($value){
$this->_mysql_password = $value;
} // function

var $_my_database;

function Getmy_database(){
return $this->_my_database;
} // function

function Setmy_database($value){
$this->_my_database = $value;
} // function

function mysql_db_optimize(){

}

function dbconnect(){
/* Connection settings */
$this->_dblink = @mysql_connect($this->_mysql_host, $this->_mysql_user, $this->_mysql_password);
@mysql_select_db($this->_my_database);
}

function configuredb($host,$user,$pass,$db){
$this->_mysqlhost = $host;
$this->_mysql_password = $pass;
$this->_mysql_user = $user;
$this->_my_database = $db;
}

function get_db_tables(){
$result = @mysql_list_tables($this->_my_database);

if (!$result) {
print "Error : Cannot connect to MySQL Database\n<br>";
print 'Error MySQL : ' . mysql_error();
exit;
}

while ($row = mysql_fetch_row($result)) {
$Tables[] = $row[0];
}
return $Tables;
}

function execute($operation = "OPTIMIZE"){
if ($operation!= "OPTIMIZE" or $operation!= "ANALYZE" or $operation!= "REPAIR") {
$operation = "OPTIMIZE";
}
$color1 = "#ooccff";
$color2 = "#ooccff";
$color3 = "#ooccff";

$Tables = $this->get_db_tables();
$query = "$operation TABLE ";
foreach($Tables as $k=>$v){
$query .= " `$v`,";
}
$query = substr($query,0,strlen($query)-1);
$result = @mysql_query($query,$this->_dblink);
$res = "<table border=\"0\" cellpadding=\"5\">\n";
$res .= "<tr>
<th bgcolor=\"$color1\">Table</th>
<th bgcolor=\"$color1\">Optimize</th>
<th bgcolor=\"$color1\">Table Status</th>
<th bgcolor=\"$color1\">Table State</th>
</tr>";
$bgcolor = $color3;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$res .= "\t<tr>\n";
foreach ($line as $col_value) {
if ($col_value == "OK") {
$optimize = " <a href=\"?operation=OPTIMIZE\">Optimize DB</a>";
} else unset($optimize);
$res .= "\t\t<td valign=\"top\" bgcolor=\"$bgcolor\">$col_value $optimize</td>\n";
}
$res .= "\t</tr>\n";
if ($bgcolor==$color3) {
$bgcolor = $color2;
} else $bgcolor = $color3;
}
$res .= "</table>\n<br>";
$res .= base64_decode('Q2xpY2sgPGEgaHJlZj0iIyIgb25DbGljaz0iaGlzdG9yeS5nbygtMSk7cmV0dXJuIHRydWU7Ij5IZXJlPC9hPiB0byBnbyBiYWNrIHRvIHRoZSBTeXN0ZW0gU2V0dGluZ3MgY29udHJvbCBwYW5lbCA8YnI+DQpQb3dlcmVkIGJ5IFdvd05ldHdvcmsuaW5mbw==');
return $res;
}
}

$db = new mysql_db_optimize;
$db->configuredb("localhost","DB USER","PASSWORD","TABLE NAME");
$db->dbconnect();

if(!isset($operation))
$operation = "ANALYZE";

$res = $db->execute($operation);
echo $res;

?>
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Other · Next Topic »
Add Reply