MySQL UTF-8

Gyakran okoz problémát, hogy adatbázisunk latin1 vagy más egzotikus kódlappal lett létrehozva.
Ezzel a scripttel az adatbázis, annak táblái, valamint a mezők kódlapját tudjuk egyszerűen átállítani:

<?php
if ($argc != 4)
{
  echo $argv[0]." <USERNAME> <PASSWORD> <DATABASE>\n";
  echo "change database table and fields collation to utf8_hungarian_ci\n";
    exit(1);
}

// Configuration Section
$server = 'localhost'; //probably localhost but change if required
$username = trim($argv[1]); // get username from commandline
$password = trim($argv[2]); // get password from commandline
$database = trim($argv[3]); // get databas name from commandline
$new_charset = 'utf8'; // change to the required character set 
$new_collation = 'utf8_hungarian_ci'; // change to the required collatio
// Connect to database
$db = mysql_connect($server, $username, $password); if(!$db) die("Cannot connect to database server -".mysql_error());
$select_db = mysql_select_db($database); if (!$select_db) die("could not select $database: ".mysql_error());

// change database collation
mysql_query("ALTER DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");

// Loop through all tables changing collation
$result=mysql_query('show tables');
while($tables = mysql_fetch_array($result)) {
$table = $tables[0];
mysql_query("ALTER TABLE $table DEFAULT CHARACTER SET $new_charset COLLATE $new_collation");

// loop through each column changing collation
$columns = mysql_query("SHOW FULL COLUMNS FROM $table where collation is not null");
while($cols = mysql_fetch_array($columns)) {
$column = $cols[0];
$type = $cols[1];
mysql_query("ALTER TABLE $table MODIFY $column $type CHARACTER SET $new_charset COLLATE $new_collation");
}

print "changed collation of $table to $new_collation\n";
}
print "\n";
print "The collation of your database has been successfully changed!\n";
?>

Vélemény, hozzászólás?

Az email címet nem tesszük közzé.

kettő × öt =