PHPBuilder - Check And Repair MYSQL



RSS Twitter
Snippets Databases

Check And Repair MYSQL

by: Alex Unger
|
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.



{\rtf1\mac\ansicpg10000\cocoartf102
{\fonttbl\f0\fswiss\fcharset77 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh9000\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural

\f0\fs24 \cf0 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\
<html>\
<head>\
<title>Check and Repair</title>\
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\
</head>\
\
<body>\
<?php\
		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);\
		\
        mysql_select_db($db);\
		\
		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.";\
?> \
</body>\
</html>\
</body>\
</html>}

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Alex Unger

Comment:



Comment:

(Maximum characters: 1200). You have characters left.