#native_company# #native_desc#

Check And Repair MYSQL

By Alex Unger
on January 24, 2004

Version: 1.0.1

Type: Full Script

Category: Databases

License: GNU General Public License

Description: This script checks for errors in your database tables and then repairs them when they are found.

It performs a check on all the tables then if it finds an error the script attempts to repair it. If the repair fails it tries to repair it with a better repair method that takes longer and may result in extra rows appearing in your table. If that repair fails then it uses its last resort and tries to rebuild the table. After it is done Checking and Repairing the table it will Optimize the table.

The script documents every step that is taken and then display the check and repair log on the screen.

This script only requires you to configure 5 things.

{fonttblf0fswissfcharset77 Helvetica;}

f0fs24 cf0 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>Check and Repair</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

		echo "Table check and repair started.<br><br>";
    /**    CONFIG                                                CHANGE HERE            **/
    /***************************************      *************************/
    /** Database Name **/						  $db = "DB Name";
    /** Host Address **/						  $host = "Address";
    /** User Name **/							  $user = "User Name";
    /** Password **/							  $password = "Password";
    /***************************************      *************************/
    /** Put table name in the quotes. **/	      table_check_repair("Table Name", $db, $host, $user, $password);
    /** Duplicate above row for all tables. **/
    /**    END CONFIG                                                                          **/

function table_check_repair($dbname, $db, $host, $user, $password)
        $conection = mysql_connect ($host, $user, $password);
		echo "The table "$dbname" was checked.<br>";
		$check = "CHECK TABLE `$dbname` EXTENDED";
        $results = mysql_db_query($db, $check, $conection);
        $results = mysql_fetch_array ($results);
        echo "Results: $results[Table] -> $results[Msg_text]<br>";
if ($results['Msg_text'] != "OK")
		echo "The table "$dbname" did not check out well. A repair was attempted.<br>";
        $repair1 = "REPAIR TABLE `$dbname` QUICK";
        $results = mysql_db_query($db, $repair1, $conection);
        $results = mysql_fetch_array ($results);
        echo "Results: $results[Table] -> $results[Msg_text]<br>";
if ($results['Msg_text'] != "OK")
		echo "The table "$dbname" was not fixed. A different repair was attempted.<br>";
        $repair2 = "REPAIR TABLE `$dbname` EXTENDED";
        $results = mysql_db_query($db, $repair2, $conection);
        $results = mysql_fetch_array ($results);
        echo "Results: $results[Table] -> $results[Msg_text]<br>";
if ($results['Msg_text'] != "OK")
		echo "The table "$dbname" was not fixed. The last repair was attempted.<br>";
        $repair3 = "REPAIR TABLE `$dbname` USE_FRM";
        $results = mysql_db_query($db, $repair3, $conection);
        $results = mysql_fetch_array ($results);
        echo "Results: $results[Table] -> $results[Msg_text]<br>";	
        $optimize = "OPTIMIZE TABLE `$dbname`";
        $results = mysql_db_query($db, $optimize, $conection);
        $results = mysql_fetch_array ($results);
		echo "The table "$dbname" was optimized.<br>";
        echo "Results: $results[Table] -> $results[Msg_text].<br><br>"; 

		echo "<br>The table check and repair has ended. Reload this page to test if the repairs worked.";