Convert MySQL Tables to UTF-8

Share your short snippets of code or complete programs here.

Moderator: Staff

Convert MySQL Tables to UTF-8

Postby pentapenguin » Fri Apr 27, 2007 5:43 am

This is a very simple little script that will change all the tables in a selected database to UTF-8. Simply save the file as change_tables.php, fill in your MySQL server name, username, password, and your database name, then upload to your server and run. Be sure to delete this when you are finished.

Code: Select all
<?php
// Fill in your configuration below
$db_server      = '<MySQL server name here>';
$db_user      = '<username here>';
$db_password   = '<database password here>';
$db_name      = '<database name here>';

// Do not change anything below this
header('Content-type: text/plain');

$connection = mysql_connect($db_server, $db_user, $db_password) or die( mysql_error() );
$db = mysql_select_db($db_name) or die( mysql_error() );

$sql = 'SHOW TABLES';
$result = mysql_query($sql) or die( mysql_error() );

while ( $row = mysql_fetch_row($result) )
{
   $table = mysql_real_escape_string($row[0]);
   $sql = "ALTER TABLE $table DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
   mysql_query($sql) or die( mysql_error() );
   print "$table changed to UTF-8 successfully.\n";
}

mysql_close($connection);
?>


Version #2 that will update the columns as well:
Code: Select all
<html>
<head>
<title>Convert MySQL Database to UTF-8</title>
</head>
<body>

<?php
// Fill in your configuration below
$db_server      = '<MySQL server name here>';
$db_user      = '<username here>';
$db_password   = '<database password here>';
$db_name      = '<database name here>';

// Do not change anything below this
set_time_limit(0);

$connection = mysql_connect($db_server, $db_user, $db_password) or die( mysql_error() );
$db = mysql_select_db($db_name) or die( mysql_error() );

$sql = 'SHOW TABLES';
if ( !($result = mysql_query($sql)) )
{
   print '<span style="color: red;">SQL Error: <br>' . mysql_error() . "</span>\n";
}

// Loop through all tables in this database
while ( $row = mysql_fetch_row($result) )
{
   $table = mysql_real_escape_string($row[0]);
   $sql2 = "ALTER TABLE $table DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
   
   if ( !($result2 = mysql_query($sql2)) )
   {
      print '<span style="color: red;">SQL Error: <br>' . mysql_error() . "</span>\n";
      
      break;
   }
   
   print "$table changed to UTF-8 successfully.<br>\n";

   // Now loop through all the fields within this table
   $sql3 = "SHOW COLUMNS FROM $table";
   if ( !($result3 = mysql_query($sql3)) )
   {
      print '<span style="color: red;">SQL Error: <br>' . mysql_error() . "</span>\n";
      
      break;
   }

   while ( $row3 = mysql_fetch_row($result3) )
   {
      $field_name = $row3[0];
      $field_type = $row3[1];
      
      // Change text based fields
      $skipped_field_types = array('char', 'text', 'blob', 'enum', 'set');
      
      foreach ( $skipped_field_types as $type )
      {
         if ( strpos($field_type, $type) !== false )
         {
            $sql4 = "ALTER TABLE $table CHANGE `$field_name` `$field_name` $field_type CHARACTER SET utf8 COLLATE utf8_bin";
            if ( !($result4 = mysql_query($sql4)) )
            {
               print '<span style="color: red;">SQL Error: <br>' . mysql_error() . "</span>\n";
               
               break 3;
            }
            print "---- $field_name changed to UTF-8 successfully.<br>\n";
         }
      }
   }
   print "<hr>\n";
}

mysql_close($connection);
?>

</body>
</html>
Last edited by pentapenguin on Sat Apr 28, 2007 2:44 am, edited 2 times in total.
pentapenguin
Retired Administrator
Retired Administrator
 
Posts: 1549
Joined: Sun Sep 03, 2006 11:56 pm
Location: Georgia, USA

Postby creswellnet » Fri Apr 27, 2007 9:18 am

If you don't mind this could probably use some explanation as to why you would want to.

For instance, does this have anything to do with why my symbols (&reg; and &copy;) don't seem to always come through in html (on IE), since my page char encoding is UTF-8?)
creswellnet
Veteran
Veteran
 
Posts: 1265
Joined: Fri Sep 08, 2006 10:54 pm
Location: Michigan, USA

Postby Swan » Fri Apr 27, 2007 2:22 pm

selected database to UTF-8....

Why would i change a database to UTF-8 ?
swan_aBohovian at yahoo dot com
User avatar
Swan
Veteran
Veteran
 
Posts: 1458
Joined: Sat Sep 09, 2006 2:11 am
Location: Central aBoho

Postby Renaissance » Fri Apr 27, 2007 2:39 pm

The request and reason can be found in this thread: convert the entire database's collation to utf8_general_ci
I am constantly working on the server. Let me know if you need something addressed.
I'd also appreciate any contributions you can make towards server costs.
Renaissance
Administrative Director
Administrative Director
 
Posts: 1428
Joined: Fri Sep 08, 2006 1:46 pm
Location: Ontario, Canada

Postby Mgccl » Fri Apr 27, 2007 5:07 pm

Usually people want to change to UTF is because some people(like me) do posts mixed with some symbols and characters from some lanuages.
Image
Image
User avatar
Mgccl
Sveitian
Sveitian
 
Posts: 54
Joined: Sat Mar 31, 2007 6:10 pm

Postby creswellnet » Fri Apr 27, 2007 5:40 pm

Mgccl wrote:Usually people want to change to UTF is because some people(like me) do posts mixed with some symbols and characters from some lanuages.

That makes sense. Since MySQL installed that way by default for me, I never looked at it.
creswellnet
Veteran
Veteran
 
Posts: 1265
Joined: Fri Sep 08, 2006 10:54 pm
Location: Michigan, USA

Postby pentapenguin » Fri Apr 27, 2007 5:42 pm

creswellnet wrote:If you don't mind this could probably use some explanation as to why you would want to.

You need to use UTF-8 if you want support for special symbols like ™ or £ or if you want support for non Latin based alphabets say like Hebrew, Japanese, etc.

creswellnet wrote:For instance, does this have anything to do with why my symbols (&reg; and &copy;) don't seem to always come through in html (on IE), since my page char encoding is UTF-8?)

Possibly though you should always use htmlentities() on output.
pentapenguin
Retired Administrator
Retired Administrator
 
Posts: 1549
Joined: Sun Sep 03, 2006 11:56 pm
Location: Georgia, USA

Postby pentapenguin » Sat Apr 28, 2007 2:45 am

Okay I updated example #2 and fixed a bug and made it HTML output so it will look better.
pentapenguin
Retired Administrator
Retired Administrator
 
Posts: 1549
Joined: Sun Sep 03, 2006 11:56 pm
Location: Georgia, USA

Postby Curtis » Sun Apr 29, 2007 10:36 pm

Just for optimization, echo is faster than print, because print evaluates success and returns true or false.

Also, mysql_query(...) or die ( mysql_error() ) will print out the error twice, because if an error occurs and you don't use @ to suppress it, mysql_query will already output mysql_error() string.

Great job on this awesome script!
"The second you're smart enough to recognize freedom, you're no longer free." -Eyedea

Random stuff: dyersweb.com
Sometimes-used Forums: In the works
Curtis
Retired Staff
Retired Staff
 
Posts: 820
Joined: Thu Sep 21, 2006 10:47 pm
Location: Penny Lane

Postby pentapenguin » Mon Apr 30, 2007 5:00 am

Curtis wrote:Just for optimization, echo is faster than print, because print evaluates success and returns true or false.

I know, I know...blame it on learning Perl first and having "print" inculcated into my skull permanently. :P

Curtis wrote:Also, mysql_query(...) or die ( mysql_error() ) will print out the error twice, because if an error occurs and you don't use @ to suppress it, mysql_query will already output mysql_error() string.

Well I'd thought of that but when I tested it by deliberately breaking the SQL (e.g. changing SELECT to xSELECT) I just got one error. Do you get two or just one?

And thanks. :)
pentapenguin
Retired Administrator
Retired Administrator
 
Posts: 1549
Joined: Sun Sep 03, 2006 11:56 pm
Location: Georgia, USA

Postby Curtis » Mon Apr 30, 2007 7:09 am

Hm, I'll have to check it out later, but I remember getting errstring repeated. Maybe not, though.
"The second you're smart enough to recognize freedom, you're no longer free." -Eyedea

Random stuff: dyersweb.com
Sometimes-used Forums: In the works
Curtis
Retired Staff
Retired Staff
 
Posts: 820
Joined: Thu Sep 21, 2006 10:47 pm
Location: Penny Lane


Return to Code Snippets and Programs

Who is online

Users browsing this forum: No registered users and 0 guests

cron