Created
March 5, 2012 23:58
-
-
Save dshafik/1982235 to your computer and use it in GitHub Desktop.
Converting MySQL tables to Unicode - from http://daveyshafik.com/archives/166-migrating-mysql-data-to-unicode.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AddDefaultCharset UTF-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET NAMES utf8; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment