Blogiversity.org

Welcome to Blogiversity.org Sign in | Join | Help
in Search
Blogiversity Links - America One unsecured loans are fast and easy : click here for a LifeLock discount

Grey's Music

MySQL: Repairing Tables/Table Repair

If you're like me your database administrator is retarded and your database constantly has corrupted tables. Rather than wasting your time every day trying to sort through thousand of tables by guessing which ones are corrupted I've written a script that you can implement to quickly sort through all of your tables and repair them if they need it. This site has around 2000 tables and millions of records but this script has never taken more than about 30 seconds to repair them all. Save yourself the hassle of trying to repair tables yourself and just use this script. You're welcome in advance. MySQL Table Repair Script
define("DB_USERNAME", "your_MySQL_username_here"); define("DB_PASSWORD", "your_MySQL_password_here"); define("DATABASE", "your_MySQL_database_name_here"); function dbConnect() { //connect to database $dbname = DATABASE; $handle = mysql_connect("localhost", DB_USERNAME, DB_PASSWORD); if (!$handle) { $today = date("F j, Y, g:i a"); exit; } mysql_select_db($dbname); return $handle; }//end db_connect() function dbDisconnect() { if (isset($handle)) { mysql_close($handle); } }//end dbDisconnect #start the list of tables $db = dbConnect(); $tables = array(); $rows = mysql_query("SHOW TABLES FROM $dbname"); while ($row = mysql_fetch_array($rows)) { $tables[] = $row[0]; } #prep array for loop foreach ($tables as $key) { $query="repair table $key"; $result = mysql_query($query); while ($row = mysql_fetch_row($result)) { for ($j=0;$j<count($row);$j++) { echo "$row[$j]\n"; echo " "; } } echo "<br />"; } $db = dbDisconnect(); ############## Ta-Da! All of the tables in your poor MySQL are repaired! If you're feeling a little more ambitious you can set this up to run on as a cron job and you'll never have to worry about your tables being corrupted again.

Comments

No Comments

Leave a Comment

You must log in first to post a comment. Click here to log in.

Not a member? Click here to sign up today!