Skip to content
This repository has been archived by the owner on Oct 22, 2020. It is now read-only.

Commit

Permalink
code cleaned
Browse files Browse the repository at this point in the history
  • Loading branch information
emamut committed Oct 13, 2016
1 parent bf466d1 commit 005a80a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions schema/change-the-collation.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
<?php
// Source: http://www.holisticsystems.co.uk/blog/?p=931

$server = 'localhost';
$username = 'user';
$password = 'password';
$database = 'database';
$new_charset = 'utf8';
$new_collation = 'utf8_general_ci';

// 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)) {
Expand All @@ -25,7 +30,7 @@
mysql_query("ALTER TABLE $table MODIFY $column $type CHARACTER SET $new_charset COLLATE $new_collation;");
mysql_query("SET FOREIGN_KEY_CHECKS=1;");
}

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

print "\n\nThe collation of your database has been successfully changed!";

0 comments on commit 005a80a

Please sign in to comment.