1

How to convert database charset to UTF8

If you need to use UTF8 charset and want to convert your database charset to UTF8 here is a simple way to do it:

  1. Make sure you backup your (If your database is new no need to think about that)
  2. Now open your notepad and Copy & Paste code below:

    [php]<?php
    // Database info

    $dbhost = "localhost";
    $dbuser = "db_user";
    $dbpass = "your_dbpass";
    $dbname = "your_dbname";

    //—————

    header("Content-type: text/plain");

    $dbconn = mysql_connect($dbhost, $dbuser, $dbpass) or die( mysql_error() );
    $db = mysql_select_db($dbname) 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, CONVERT TO CHARACTER SET utf8";
    mysql_query($sql) or die( mysql_error() );
    print "$table changed to UTF-8.\n";
    }

    mysql_close($dbconn);
    ?>[/php]

  3. Edit database username & password data in the code.
  4. Save that file as convert2Utf8.php and upload to your site’s base/admin directory.
  5. Browse that file using your browser (Eg. http://myserver.com/convert2Utf8.php)
  6. Check for the results..

This simple php script converts all the tables to UTF8.

Cheers!

One Comment

  1. Anonymous :

    thanks this helped a lot

One Trackback

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*