MySQL – OPTIMIZE all tables with PHP and mysqli
$alltables = mysqli_query($conn, "SHOW TABLES"); while ($table = mysqli_fetch_assoc($alltables)){ foreach ($table as $db => $tablename){ echo "OPTIMIZE TABLE `".$tablename."`"; mysqli_query($conn, "OPTIMIZE TABLE `".$tablename."`") or die(mysqli_error()); echo "<br>"; } }