Location: PHPKode > scripts > mySQL Table Tool > table_tool.php
<?php
##########################################################################
#  mySQL Table Tool Version 1.00
#
#
#  Created:		Aug 08, 2010
#  Updated:		Aug 08, 2010
#  Copyright:	2010 Virtual Solutions. All Rights Reserved
#
#  URL:			http://www.monster-submit.com/mysql_table_tool/
##########################################################################

##########################################################################
#  Copyright Notice
##########################################################################
#  Any redistribution of this script without the expressed written consent
#  of Virtual Solutions is strictly prohibited. Copying any of the code
#  contained within this script and claiming it as your own is also
#  prohibited. You may not remove any of these header notices. By using
#  this code you agree to indemnify Virtual Solutions from any liability
#  that might arise from its use.

##########################################################################
#  Installation
##########################################################################
#  1. Rename file with .PHP file extension.
#  2. Define $db_host, $db_usr, $db_pwd below.
#  3. Upload to web server (Either upload to password protected directory,
#     or delete from web server after done)
#  4. Call script from web browser.

$db_host	= 'localhost';
$db_usr		= '';
$db_pwd		= '';

##########################################################################
#  Do NOT change or alter the code below!
##########################################################################

session_start();
ini_set(mysql.connect_timeout, 10);
$conn		= @mysql_connect($db_host, $db_usr, $db_pwd);
if (! $_POST['submit'] and ! $_GET['submit']) {
	$res		= @mysql_list_dbs($conn);
	unset($dbs);
	while ($row = @mysql_fetch_object($res)) { $dbs[] = $row->Database; }
	sort($dbs);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>mySQL Table Tool</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.caption {
	font-size:		9pt;
	font-family:	arial, sans-serif;
	line-height:	11pt;
}
fieldset {
	width:			500px;
	position:		relative;
	border:			1px solid #781351;
}
legend {
	color:			#FFF;
	background:		#FFA20C;
	border:			1px solid #781351;
	padding:		2px 6px;
}
</style>
</head>
<body>
<form action="" method="post" target="results" name="submission">
<table align="center" width="800">
	<tr>
		<td>
		<table border="0" style="border:1px solid green;" width="100%" bgcolor="#EAEAEA" align="center">
			<tr>
				<td colspan="3" bgcolor="red" align="center"><font color="#FFFFFF"><b>mySQL Table Tool Version 1.00</b></font></td>
			</tr>
			<tr>
				<td width="200" style="white-space:nowrap;"><b>Select Database:</b></td>
				<td>
				<select id="dbs[]" name="dbs[]" size="10" multiple="multiple">
					<?php
					foreach ($dbs as $key => $val) {
						if ($val) {
							echo "			<option value=\"" . $val . "\">" . $val . "</option>\n";
						}
					}
					?>
				</select>
				</td>
				<td valign="top">
				<textarea name="" cols="80" rows="10">It is HIGHLY recommended that you backup your database (e.g., via phpMyAdmin) before you run this tool.

Product: mySQL Table Tool
Author: Virtual Solutions
License: Copyright (c) 2010 - <?php echo date('Y'); ?> Virtual Solutions

Any redistribution of this application without the expressed written consent of Virtual Solutions is strictly prohibited. Copying any of the code contained within this application and claiming it as your own is also prohibited. You may not remove any header notices from this application. By using this application, you agree to indemnify Virtual Solutions from any liability that might arise from their use.

THIS APPLICATION IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</textarea>
				</td>
			</tr>
			<tr>
				<td width="200" style="white-space:nowrap;"><b>Repair:</b></td>
				<td colspan="2"><input type="checkbox" name="repair" value="1" /></td>
			</tr>
			<tr>
				<td width="200" style="white-space:nowrap;"><b>Optimize:</b></td>
				<td colspan="2"><input type="checkbox" name="optimize" value="1" /></td>
			</tr>
			<tr>
				<td width="200" style="white-space:nowrap;"><b>Number Tables Per Batch</b></td>
				<td colspan="2"><input type="text" name="npp" size="5" maxlength="5"></td>
			</tr>
			<tr>
				<td colspan="3" align="center"><input type="submit" name="submit" value="submit" /></td>
			</tr>
			<tr>
				<td colspan="3" align="center">
				<iframe style="background-color:#FFFFFF;" name="results" src="" width="100%" height="300"></iframe>
				</td>
			</tr>
			<tr>
				<td colspan="3" align="center"><b>&copy; Copyright 2010 - <?php echo date('Y'); ?> <a href="http://www.monster-submit.com/mysql_table_tool/">Virtual Solutions</a>. All Rights Reserved.</b></td>
			</tr>
		</table>
	</tr>
</table>
</form>
</body>
</html>
<?php
}
else {
	$c			= ($_POST['c']) ? $_POST['c'] : $_GET['c'];
	if (! $c) { $c = 0; }
	if ($_POST['submit'] == "submit") { $_SESSION['repair'] = $_POST['repair']; }
	if ($_POST['submit'] == "submit") { $_SESSION['optimize'] = $_POST['optimize']; }
	if ($_POST['submit'] == "submit") { $_SESSION['npp'] = $_POST['npp']; }
	if ($_POST['submit'] == "submit") { $_SESSION['dbs'] = $_POST['dbs']; }
	$repair		= ($_POST['repair'] ? $_POST['repair'] : $_SESSION['repair']);
	$optimize	= ($_POST['optimize'] ? $_POST['optimize'] : $_SESSION['optimize']);
	$npp		= ($_POST['npp'] ? $_POST['npp'] : $_SESSION['npp']);
	$dbs		= ($_POST['dbs'] ? $_POST['dbs'] : $_SESSION['dbs']);
	unset($err);
	if (! $dbs) { $err .= "<font color='red'>No [Databases] selected!</font><br/>\n"; }
	if (! $repair and ! $optimize) { $err .= "<font color='red'>No action (Repair or Optimize) selected!</font><br/>\n"; }
	if ($npp and ! preg_match('/^\d+$/', $npp)) { $err .= "<font color='red'>Invalid value for [Number Tables Per Batch] entered!</font><br/>\n"; }
	if (! $npp) { $err .= "<font color='red'>No required value for [Number Tables Per Batch] entered!</font><br/>\n"; }
	if ($err) {
		echo $err;
		exit;
	}
	$i			= 0;	
	foreach ($dbs as $key => $db) {
		$res	= @mysql_query("SHOW TABLE STATUS FROM `" . $db . "`", $conn);
		while ($data = @mysql_fetch_array($res)) {
			$array[$i]['database']	= $db;
			$array[$i]['table']		= $data['Name'];
			$array[$i]['Data_free']	= $data['Name'];
			$i++;
		}
	}
	$limit	= count($array);
	$array	= array_slice($array, $c, $npp);
	if ($c >= $limit) {
		echo "Done!";
		unset($array, $dbs, $_GET, $_POST, $_SESSION);
		exit;
	}
	foreach ($array as $key => $val) {
		$db	= $val['database'];
		if ($repair) {
			echo "<li>Repairing [$db." . $val['table'] . "] Table...";
			@mysql_query("REPAIR TABLE `" . $val['taable'] ."`", $conn);
			echo " <span style='background-color:green;color:#FFFFFF;'>Done!</span></li>\n";
			$c++;
		}
		else {
			echo "<li>Repair [$db." . $val['table'] . "] Table... <span style='background-color:#EAEAEA;color:#000000;'>Suppressed!</span></li>\n";
		}
		if ($optimize) {
			if ($val['Data_free'] > 0) {
				echo "<li>Optimizing [$db." . $val['table'] . "] Table...";
				@mysql_query("OPTIMIZE TABLE `" . $val['table'] ."`", $conn);
				echo " <span style='background-color:green;color:#FFFFFF;'>Done!</span></li>\n";
			}
			else {
				echo "<li>Optimize [$db." . $val['table'] . "] Table... <span style='background-color:#EAEAEA;color:#000000;'>Not Required!</span></li>\n";
			}
			$c++;
		}
		else {
			echo "<li>Optimize [$db." . $val['table'] . "] Table... <span style='background-color:#EAEAEA;color:#000000;'>Suppressed!</span></li>\n";
		}
	}
	$u	= $_SERVER['PHP_SELF'] . "?c=$c&submit=submit";
	echo "<meta http-equiv='refresh' content='2;url=$u'>\n";
	unset($array, $dbs, $_GET, $_POST);
}
?
Return current item: mySQL Table Tool