|
|
|
|
|
|
| |
"MySQL is probably the most popular database being used with PHP applications. Sometimes, the databases need to be subject of maintenance tasks like optimizing heavily changed tables or repair tables that may have been affected by some kind of system malfunctioning.
MySQL provides seperate tools to perform these tasks but in the latest versions it was added the possibility request the execution of this kind of operatings via special SQL commands, but on a table by table basis.
This class provides an interface to automate the execution of these maintenance tasks on all tables of a given database.
This is a simple level of automation enables for instance the possibility to schedule the database maintenance operations by executing PHP scripts that use this class to execute the necessary maintenance tasks without human intervention."
Manuel Lemos
mysqlop.class.php |
[code]
<?php
/**
* class mysql_db_optimize : Analyse and optimise mysql database
*
*
* @version 1.0.0
* @author Ben Yacoub Hatem <hatem@php.net>
* @since 26.09.2003 12:30:24
* @access public
*/
class mysql_db_optimize{
/**
*
* @access private
**/
var $_dblink;
/**
* Get accessor for $_dblink property
*
* @access public
**/
function Getdblink(){
return $this->_dblink;
} // function
/**
* Set accessor for $_dblink property
*
* @access public
**/
function Setdblink($value){
$this->_dblink = $value;
} // function
/**
*
* @access private
**/
var $_mysqlhost;
/**
* Get accessor for $_mysqlhost property
*
* @access public
**/
function Getmysqlhost(){
return $this->_mysqlhost;
} // function
/**
* Set accessor for $_mysqlhost property
*
* @access public
**/
function Setmysqlhost($value){
$this->_mysqlhost = $value;
} // function
/**
*
* @access private
**/
var $_mysql_user;
/**
* Get accessor for $_mysql_user property
*
* @access public
**/
function Getmysql_user(){
return $this->_mysql_user;
} // function
/**
* Set accessor for $_mysql_user property
*
* @access public
**/
function Setmysql_user($value){
$this->_mysql_user = $value;
} // function
/**
*
* @access private
**/
var $_mysql_password;
/**
* Get accessor for $_mysql_password property
*
* @access public
**/
function Getmysql_password(){
return $this->_mysql_password;
} // function
/**
* Set accessor for $_mysql_password property
*
* @access public
**/
function Setmysql_password($value){
$this->_mysql_password = $value;
} // function
/**
*
* @access private
**/
var $_my_database;
/**
* Get accessor for $_my_database property
*
* @access public
**/
function Getmy_database(){
return $this->_my_database;
} // function
/**
* Set accessor for $_my_database property
*
* @access public
**/
function Setmy_database($value){
$this->_my_database = $value;
} // function
/**
* Constructor
* @access protected
*/
function mysql_db_optimize(){
}
/**
* method dbconnect : connect to mysql db
*
* @access public
* @return void
**/
function dbconnect(){
/* Connexion et sélection de la base */
$this->_dblink = @mysql_connect($this->_mysql_host, $this->_mysql_user, $this->_mysql_password);
@mysql_select_db($this->_my_database);
}
/**
* method configuredb : configure mysql database
*
* @access public
* @return void
**/
function configuredb($host,$user,$pass,$db){
$this->_mysqlhost = $host;
$this->_mysql_password = $pass;
$this->_mysql_user = $user;
$this->_my_database = $db;
}
/**
* method get_db_tables : return list of tables
*
* @access public
* @return void
**/
function get_db_tables(){
$result = @mysql_list_tables($this->_my_database);
if (!$result) {
print "Erreur : impossible de lister les bases de données\n";
print 'Erreur MySQL : ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
$Tables[] = $row[0];
}
return $Tables;
}
/**
* method execute : execute an operation in all db table
*
* @param $operation operation to execute on DB
*
* @access public
* @return void
**/
function execute($operation = "OPTIMIZE"){
if ($operation!= "OPTIMIZE" or $operation!= "ANALYZE" or $operation!= "REPAIR") {
$operation = "OPTIMIZE";
}
$color1 = "#F57E1D";
$color2 = "#F5F6C4";
$color3 = "#FFFF40";
$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\">Op</th>
<th bgcolor=\"$color1\">Msg_type</th>
<th bgcolor=\"$color1\">Msg_text</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";
$res .= base64_decode('PGJyPjxzbWFsbD48YSBocmVmPSJodHRwOi8vd3d3LmR5bmFtaXgtdG4uY29tIj5Qb3dlcmVkIGJ5IER5bmFtaXg8L2E+PC9zbWFsbD4=');
return $res;
}
}
?> | |
Usage Example
| $db = new mysql_db_optimize;
/*
* Configurer la connexion א la base de donnיes.
*/
$db->configuredb("localhost","db_login","db_pass","ma_base");
$db->dbconnect();
/**
* Modifier la valeur de la variable operation selon :
*
* ANALYZE : analyser toutes les tables dans la base de donnיes
* OPTIMIZE : optimiser toutes les tables dans la base de donnיes
* REPAIR : rיparer toutes les tables dans la base de donnיes
*/
if(!isset($operation))
$operation = "ANALYZE";
$res = $db->execute($operation);
echo $res;
?> | | |
|
| phpFormGenerator for Dynamic Form Generation from MySQL Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP | | | Create and restore backup of MySQL databases Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs | | | MySQL Class to ease Database connectivity Categories : MySQL, PHP Classes, Databases, PHP | | | DBXML- A Class to backup databases in XML Format using web interface Categories : PHP, PHP Classes, Databases, MySQL, XML | | | This class splits the results of the query into multiple pages like what the search engine does. Categories : PHP Classes, PHP, MySQL, Databases | | | MySQL database class Categories : PHP, MySQL, Databases, PHP Classes | | | Ajax PHP Tree (Left and Right) with MySQL Categories : PHP, Databases, MySQL, AJAX, PHP Classes | | | YellowPages Content Grabber (PHP5 +) Categories : PHP, PHP Classes, Regexps, Databases, MySQL | | | TAB_STRUCT Class: Is supporting Class for the DBXML Class Categories : PHP, PHP Classes, MySQL, XML, Databases | | | Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP. Categories : PHP Classes, Databases, PHP, MySQL, InnoDB | | | PHP Transfer data from text file to Mysql Table Categories : PHP, PHP Classes, Filesystem, Databases, MySQL | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | |
|
|