Version: 1.0

Type: Full Script

Category: Databases

License: Other

Description: This little script crawls through a MySQL server and tries to repair all tables. Easyly costomizable, clear output with all errors, waning etc.



<?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.02 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 <sascha.carlin@nmail.de>                      |
// +----------------------------------------------------------------------+
//
// $Id: repair.php,v 0.1 2001/08/11 02:26:00 $

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

// initialize the status counters counting errors, warnings and infos
// for further information about the statuses
// please refer to the MySQL Reference about the REPAIR syntax
$errors   = 0;
$warnings = 0;
$infos    = 0;

// 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 
$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++) {
    echo "Repair of database $db_names[$i]<br>";

    // 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++;
    }
    echo "&nbsp;&nbsp;".count ($table_names)." table(s) found<br>";

    // start repairing the tables
    mysql_select_db ($db_names[$i]);
    for ($k = 0; $k < count ($table_names); $k++) {
        echo "&nbsp;&nbsp;repairing $table_names[$k]:&nbsp;";

        // do the 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 $status["Msg_text"]."<br>";
        
        // counting errors, warnings and infos
        switch ($status["Msg_type"]) {
            case "error":
                $errors++;
                break;
            case "warning":
                $warnings++;
                break;
            case "info":
                $infos++;
                break;
        } // end switch
    } // end table for loop
} // end database for loop

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