Skip to content

Instantly share code, notes, and snippets.

@MarkGavalda
Created April 3, 2016 14:58
Show Gist options
  • Save MarkGavalda/8b127703e60247c2a6871fe4489e7d0f to your computer and use it in GitHub Desktop.
Save MarkGavalda/8b127703e60247c2a6871fe4489e7d0f to your computer and use it in GitHub Desktop.
PHP conversion script from MyISAM to InnoDB for a WordPress site
<?php
require_once( 'wp-config.php' );
$db = array();
$db['host'] = DB_HOST;
$db['user'] = DB_USER;
$db['password'] = DB_PASSWORD;
$db['database'] = DB_NAME;
$db['connectString'] = $db['host'];
$mysqli = @new mysqli($db['connectString'], $db['user'], $db['password'], $db['database']);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error ."\n";
die(1);
}
$results = $mysqli->query("show tables;");
if ($results===false or $mysqli->connect_errno) {
echo "MySQL error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error ."\n";
die(2);
}
while ($row= $results->fetch_assoc()) {
$sql = "SHOW TABLE STATUS WHERE Name = '{$row['Tables_in_' . $db['database']]}'";
$thisTable = $mysqli->query($sql)->fetch_assoc();
if ($thisTable['Engine']==='MyISAM') {
$sql = "alter table " . $row['Tables_in_' . $db['database']]. " ENGINE = InnoDB;";
echo "Changing {$row['Tables_in_' . $db['database']]} from {$thisTable['Engine']} to InnoDB.\n";
$mysqli->query($sql);
} else {
echo $row['Tables_in_' . $db['database']] . ' is of the Engine Type ' . $thisTable['Engine'] . ".\n";
echo "Not changing to InnoDB.\n\n";
}
}
die(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment