---
title: "How to Convert a Database to UTF-8"
description: "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,..."
url: https://www.inmotionhosting.com/support/server/databases/how-to-convert-a-database-to-utf-8/
date: 2011-08-23
modified: 2022-03-11
author: "Brad Markle"
categories: ["Working with Databases"]
type: post
lang: en
---

# How to Convert a Database to UTF-8

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 “*https://example.com/convert.php*“.

Don’t forget to replace example.com 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

<?php  

// 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](https://dev.mysql.com/doc/refman/5.0/en/charset-mysql.html)

If you need further assistance please feel free to [contact our support department](/support/amp/how-to-get-great-technical-support/).
