// Change these four variables to match your configuration.
// You will probably only need to change “user”, “pwd” and “dbName”.
$server = ”localhost”;
$user = ”mysql_user”;
$pwd = ”mysql_password”;
$dbName = ”mysql_dbName”;
$link = mysql_connect($server, $user, $pwd);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($dbName, $link);
if (!$db_selected) {
die ('Can\'t use $dbName : ' . mysql_error());
}
// Find the names of all tables in the selected db.
$alltables = mysql_query("SHOW TABLES");
// Go through all tables.
while ($table = mysql_fetch_assoc($alltables))
{
foreach ($table as $db => $tablename)
{
// Perform the magic “OPTIMIZE” on every table in the db.
mysql_query("OPTIMIZE TABLE '".$tablename."'")
or die(mysql_error());
}
}
mysql_close($link);
?>