Project

General

Profile

Bug #17437 » db2utf8.php

Administrator Admin, 2007-10-18 14:23

 
<?php
/* Provider: philip.almeida@gmail.com
*
* NOTE:
* This Script was taken from altered so it converts table type too(eg myisam):
* http://www.0ext.net/news+article.storyid+3.web2
*
*/
$dbHost = "localhost";
$dbUser = "dbname";
$dbPass = "dbpass";
$charSet = "utf8";
$collate = "utf8_general_ci";
$type = "myisam";
$dbName = "dbname";

$link = @mysql_connect($dbHost, $dbUser, $dbPass);
if (!$link) {
die(mysql_errno() . ": " . mysql_error());
}
mysql_select_db($dbName) or die(mysql_errno() . ": " . mysql_error());
echo "Connected successfully.\n";

//set default charset
mysql_query("SET NAMES UTF-8", $link);
mysql_query("SET CHARACTER SET UTF-8", $link);

$sql = "SHOW TABLES FROM $dbName";
$result = mysql_query($sql);
if (!$result) {
echo mysql_errno() . ": " . mysql_error();
exit;
}

echo "Trying to applay the utf8-patch.\n";
$sql = "ALTER DATABASE $dbName CHARACTER SET UTF8 COLLATE utf8_bin";
$patchResult = mysql_query($sql);
if(!$patchResult) {
echo "Failed to convert database $dbName: " . mysql_errno() . ": " . mysql_error() . "\n";
}

while ($row = mysql_fetch_row($result)) {
//convert table
$sql = "ALTER TABLE {$row[0]} CONVERT TO CHARACTER SET $charSet COLLATE $collate";
$patchResult = mysql_query($sql);
if(!$patchResult) {
echo "Patch for the table $dbName.{$row[0]} failed: " . mysql_errno() . ": " . mysql_error() . "\n";
continue;
}
echo "Patch for the table $dbName.{$row[0]} applied\n";
//convert table
$sql = "ALTER TABLE {$row[0]} ENGINE = MYISAM ";
$patchResult = mysql_query($sql);
if(!$patchResult) {
echo "Patch for the table $dbName.{$row[0]} failed: " . mysql_errno() . ": " . mysql_error() . "\n";
continue;
}
echo "Patch for the table $dbName.{$row[0]} applied\n";
}
mysql_close($link);
?>
(2-2/3)