<html><head>
<title>Repair by sc@itst.org, v 0.2</title>
</head><body>
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Sascha Carlin <sc@itst.org>                      |
// +----------------------------------------------------------------------+
//
// $Id: repair.php,v 0.2 2002/03/02 15:20:00 $

// settings. edit to meet your needs
// ------------------------------------------------------------------------
$host "localhost:3306";
$user "schnuffie";
$pass "butterfly";
// ------------------------------------------------------------------------

// initialize the status counters counting errors, warnings and infos
$errors   0;
$warnings 0;
$infos    0;

// counting errors, warnings and infos
function counter () {
global 
$status;
global 
$errors;
global 
$warnigs;
global 
$infos;

    switch (
$status["Msg_type"]) {
        case 
"error":
            echo 
"<font color=red>".$status["Msg_text"]."</font>";
            
$errors++;
            break;
        case 
"warning":
            echo 
"<font color=blue>".$status["Msg_text"]."</font>";
            
$warnings++;
            break;
        case 
"info":
            echo 
"<font color=blue>".$status["Msg_text"]."</font>";
            
$infos++;
            break;
        default:
            echo 
$status["Msg_text"];
            break;
    } 
// end switch
}

// connect to mysql server
$dl mysql_connect ($host$user$pass) or die ("server not accessible. check if online and look at the script's settings");

// fetch database list
$result mysql_list_dbs ($dl) or die ("server error: ".mysql_errno ($dl)."<br>MySQL said: ".mysql_error($dl));
$i 0;
while (
$i mysql_num_rows ($result)) {
    
$db_names[$i] = mysql_tablename ($result$i);
    
$i++;
}
echo 
count ($db_names)." databases found<br>";

// start the repairing
for ($i 0$i count ($db_names); $i++) {
    
// flush the array containing the table names.
    
unset ($table_names);

    echo 
"<br>Checking database $db_names[$i]\n";
    echo 
"<table width=780>\n";
    echo 
"<thead><tr bgcolor=#cccccc><td width=300><b>Table Name</b></td><td width=240><b>REPAIR</b></td><td width=240><b>OPTIMIZE</b></td></tr></thead>\n";
    
// get the table names
    
$result mysql_list_tables ($db_names[$i]);
    
$j 0;
    while (
$j mysql_num_rows ($result)) {
        
$table_names[$j] = mysql_tablename ($result$j);
        
$j++;
    }

    
// dive into database.
    
mysql_select_db ($db_names[$i]);

    echo 
"<tbody>";
    
// repair.
    
for ($k 0$k count ($table_names); $k++) {
        if ((
$k 2) == 0) {
            echo 
"<tr>";
        } else {
            echo 
"<tr bgcolor=#eeeeee>";
        }
        echo 
"<td width=300 valign=top>$table_names[$k]</td>";

        
// repair.
        
$result mysql_query ("REPAIR table $table_names[$k]") or
           die (
"abnormal error ".mysql_errno ($dl)."<br>MySQL said: ".mysql_error($dl));

        
// echo mysql message
        
$status mysql_fetch_array ($result);
        echo 
"<td width=240 valign=top>";
        echo 
counter();
        echo 
"</td>\n";

        
// optimize.
        
$result mysql_query ("OPTIMIZE table $table_names[$k]") or
           die (
"abnormal error ".mysql_errno ($dl)."<br>MySQL said: ".mysql_error($dl));

        
// echo mysql message
        
$status mysql_fetch_array ($result);
        echo 
"<td width=240 valign=top>";
        echo 
counter ();
        echo 
"</td></tr>\n";

    } 
// end table for loop
    
echo "</tbody></table>\n";
// end database for loop

// close connection to server and fini
mysql_close ($dl);
echo 
"<br><br><b>Summary</b><br>done with $errors errors, $warnings warnings and $infos infos.";
?>
</body></html>