The mysql query to list the tables from a mysql database is show tables from `database` where `database` must be replace with the actual name of the database. The query lists the non-TEMPORARY
tables in a given database.
You can use inside php like this:
$res=mysql_query("show tables from `database`") or die(mysql_error()); while($r=mysql_fetch_row($res))
{ $resOpt=mysql_query("optimize table `database`.`".$r[0]."`") or die(mysql_error());
}
where optimize table is a mysql query that should be used if you have deleted a large part of a table or if you have made many changes to a table with variable-length rows. Deleted rows are maintained in a linked list and subsequent insert operations reuse old row positions. You can use optimize table to reclaim the unused space and to defragment the data file. After extensive changes to a table, this statement may also improve performance of statements that use the table, sometimes significantly.
More information about the show tables can be used here: http://dev.mysql.com/doc/refman/5.0/en/show-tables.html
More information about the optimize table can be used here: http://dev.mysql.com/doc/refman/5.1/en/optimize-table.html