How optimize all tables using php script

Optimizing tables is very necessary for database. That will improve the your website performance.  Optimize command is helpful to manage database files in right manner. This command will optimize the size of file size of database files.

How optimize all tables using  php script

How optimize all tables using  php script

How optimize all tables using php script

If you want to optimize the all tables of database then use following script.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
$dbconnect = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbconnect) {
 die('Could not connect: ' . mysql_error());
echo 'Connected to database successfully';


$alltables = mysql_query("SHOW TABLES");

while ($table = mysql_fetch_assoc($alltables))
 foreach ($table as $db => $tablename)
 mysql_query("OPTIMIZE TABLE '".$tablename."'")
 or die(mysql_error());
echo 'All tables optimized successfully';


The MySQL Optimize Table command will effectively de-fragment a mysql table and is very useful for tables which are frequently updated and/or deleted.

Add a Comment

Your email address will not be published. Required fields are marked *