InMotion Hosting Support Center

The following mini script is used to convert existing database tables to UTF-8. Upload the script to your account as "convert.php" and modify the database connection parameters and the character set, then execute the script.

To execute the script, you simply would visit the script in any web browser. If you upload the file to your public_html folder you'd visit "".

Don't forget to replace with your actual domain name. Also, to get your languages to work on your site the collation will need to be utf8.

Code to convert your database to UTF-8

// Fill in your Server, User, Database, Password, and Collation configuration below   
$db_server = 'localhost';   
$db_user = 'database user';   
$db_password = 'password';   
$db_name = 'database name';   
$char_set = 'new character set';  

// Adds the header information header('Content-type: text/plain');
// Connects to the MySQL database   $connection = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error() );   $db = mysql_select_db($db_name) or die( mysql_error() );
// Runs the SQL query on teh database   $sql = 'SHOW TABLES'; $result = mysql_query($sql) or die( mysql_error() );
// Runs a loop that finds all collations within the database and changes it to the new collation   while ( $row = mysql_fetch_row($result) ) { $table = mysql_real_escape_string($row[0]); $sql = "ALTER TABLE $table CONVERT TO CHARACTER SET $char_set COLLATE utf8_general_ci"; mysql_query($sql) or die( mysql_error() );   print "$table changed successfully.\n"; }  
// Update the Collation of the database itself $sql = "ALTER DATABASE CHARACTER SET $char_set;"; mysql_query($sql) or die( mysql_error());   print "Database collation has been updated successfully.\n";  
// close the connection to the database mysql_close($connection);  

Note! You can use this script to change the database to any character set you wish. You need to define the character set in the script to change character sets:

$char_set = 'character set';

You will need the change the utf8_general_ci to match the character set you defined in the step above. So, if you want to change the character set to "Hebrew" you'd change the line to:

$sql = "ALTER TABLE $table CONVERT TO CHARACTER SET $char_set COLLATE hebrew_general_ci";

For more information on MySQL character sets and collation please see the following link:

Character Sets and Collations in MySQL

If you need further assistance please feel free to contact our support department.

Was this article helpful?

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Forum Login

You are NOT logged in. You can still browse our Support Center.

To participate within our Community Support Forum:

n/a Points
2019-04-03 5:27 am

Hi All,

Good day.

I just want to ask if there is a way or syntax to convert this kind of unicode


to chinese characters in SQL.

Thank you in advance.



42,829 Points
2019-04-03 9:12 am
Hello Aman,

Thanks for the question on converting the Unicode to Chinese. You can check out this link for an example. If you use a search engine you will also find several websites that just convert it for you. I hope that helps to answer your question! If you require further assistance please let us know!

Arnel C.
n/a Points
2015-12-02 5:39 pm

This just saved my sanity. Thank you!!!!!

n/a Points
2015-07-15 2:49 pm

If $char_set != "utf8", should it read like this? 

$sql = "ALTER TABLE $table CONVERT TO CHARACTER SET $char_set COLLATE {$char_set}_general_ci"; 

42,829 Points
2015-07-16 11:16 am
Hello MgFrobozz,

Check out this post on the issue, as it may provide what you're searching for.

If you have any further questions or comments, please let us know.

Arnel C.
n/a Points
2014-04-12 9:10 am


I tried to update my Moodle yesterday but failed because the database was not UTF-8. A Google search found this little routine and it has worked flawlessly. Both database and Moodle have been successfully upgraded. Kudos to Brad Markle!

One small point - Moodle recommends utf8_unicode_ci collation rather than utf8_general_ci, so that's what I used.

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

6 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail:
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!