Skip to content

Instantly share code, notes, and snippets.

@dshafik
Created March 5, 2012 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dshafik/1982235 to your computer and use it in GitHub Desktop.
Save dshafik/1982235 to your computer and use it in GitHub Desktop.
<?php
$pdo = new PDO("mysql:host=HOSTNAME", "USERNAME", "PASSWORD");
$query = $pdo->prepare("SHOW DATABASES");
$query->execute();
foreach ($query->fetchAll(PDO::FETCH_OBJ) as $row) {
$db = $row->Database;
echo "= $db =" . PHP_EOL;
$pdo->exec("USE $db");
// Make sure it's a database for our application
$query = $pdo->prepare("SELECT * FROM_SOME_UNIQUE_TABLE");
$result = $query->execute();
if ($result) {
$query = $pdo->prepare("SHOW TABLES");
$result = $query->execute();
foreach ($query->fetchAll(PDO::FETCH_BOTH) as $row) {
$table = $row[0];
echo "== $table ==", PHP_EOL;
echo "Grabbing Schema: ";
$query = $pdo->prepare("SHOW CREATE TABLE $table");
$result = $query->execute();
if (!$result) {
exit;
}
echo " Done!", PHP_EOL;
echo "Fixing Schema and Creating Temporary Table: ";
$find = array("latin1", $table);
$replace = array("utf8", "{$table}_utf8");
$schema = str_replace($find, $replace, $query->fetchColumn(1));
$query = $pdo->prepare($schema);
$result = $query->execute();
if (!$result) {
exit;
}
echo " Done!", PHP_EOL;
echo "Copying Data: ";
$query = $pdo->prepare("INSERT INTO {$table}_utf8 SELECT * FROM $table");
$result = $query->execute();
if (!$result) {
exit;
}
echo " Done!", PHP_EOL;
echo "Deleting Original Table: ";
$query = $pdo->prepare("DROP TABLE $table");
$result = $query->execute();
if (!$result) {
exit;
}
echo " Done!", PHP_EOL;
echo "Renaming Temporary Table: ";
$query = $pdo->prepare("ALTER TABLE {$table}_utf8 RENAME TO $table");
$result = $query->execute();
if (!$result) {
exit;
}
echo " Done!", PHP_EOL;
}
} else {
echo "Skipping!", PHP_EOL;
}
}
?>
AddDefaultCharset UTF-8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment