{"id":143,"date":"2011-08-23T19:16:11","date_gmt":"2011-08-23T23:16:11","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2011\/08\/23\/how-to-convert-a-database-to-utf-8\/"},"modified":"2022-03-11T10:52:20","modified_gmt":"2022-03-11T15:52:20","slug":"how-to-convert-a-database-to-utf-8","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/","title":{"rendered":"How to Convert a Database to UTF-8"},"content":{"rendered":"<p>The following mini script is used to convert existing database tables to <strong>UTF-8<\/strong>. Upload the script to your account as &#8220;<em>convert.php<\/em>&#8221; and modify the database connection parameters and the character set, then execute the script.<\/p>\n<p>To execute the script, you simply would visit the script in any web browser. If you upload the file to your <strong>public_html<\/strong> folder you&#8217;d visit &#8220;<em>https:\/\/example.com\/convert.php<\/em>&#8220;.<\/p>\n<p class=\"alert\">Don&#8217;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 <strong>utf8<\/strong>.<\/p>\n<h2>Code to convert your database to UTF-8<\/h2>\n<pre>&lt;?php &nbsp; \n\n\/\/ Fill in your Server, User, Database, Password, and Collation configuration below\n\n    $db_server = 'localhost';    \n    $db_user = 'database user';  \n  \n    $db_password = 'password';   \n \n    $db_name = 'database name';  \n  \n    $char_set = 'new character set';   \n \n \/\/ Adds the header information header('Content-type: text\/plain');   \n \/\/ Connects to the MySQL database &nbsp;                             \n\n    $connection = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error() );   &nbsp;   \n    $db = mysql_select_db($db_name) or die( mysql_error() );  \n \n \/\/ Runs the SQL query on teh database  &nbsp;   \n\n    $sql = 'SHOW TABLES'; $result = mysql_query($sql) or die( mysql_error() );\n\n \/\/ Runs a loop that finds all collations within the database and changes it to the new collation &nbsp; \n\n       while ( $row = mysql_fetch_row($result) )   \n        { &nbsp;\n            $table = mysql_real_escape_string($row[0]);    \n            $sql = \"ALTER TABLE \n            $table CONVERT TO CHARACTER SET \n            $char_set COLLATE utf8_general_ci\";\n                mysql_query($sql) or die( mysql_error() ); &nbsp;\n                print \"$table changed successfully.n\";      \n         }  &nbsp; \n\n \/\/ Update the Collation of the database itself  \n\n    $sql = \"ALTER DATABASE CHARACTER SET $char_set;\";  \n            mysql_query($sql) or die( mysql_error());  &nbsp;  \n            print \"Database collation has been updated successfully.n\";  &nbsp;    \n \/\/ close the connection to the database   mysql_close($connection);  &nbsp;       \n \n ?&gt;  \n\n<\/pre>\n<p class=\"alert\"><strong>Note!<\/strong> 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:<\/p>\n<p class=\"code_block\">$char_set = &#8216;character set&#8217;;<\/p>\n<p>You will need the change the <strong>utf8_general_ci<\/strong> to match the character set you defined in the step above. So, if you want to change the character set to &#8220;<em>Hebrew<\/em>&#8221; you&#8217;d change the line to:<\/p>\n<p class=\"code_block\">$sql = &#8220;ALTER TABLE $table CONVERT TO CHARACTER SET $char_set COLLATE hebrew_general_ci&#8221;;<\/p>\n<p>For more information on MySQL character sets and collation please see the following link:<\/p>\n<p style=\"margin-left: 20px;\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/charset-mysql.html\"> Character Sets and Collations in MySQL<\/a><\/p>\n<p>If you need further assistance please feel free to <a href=\"\/support\/amp\/how-to-get-great-technical-support\/\">contact our support department<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following mini script is used to convert existing database tables to UTF-8. Upload the script to your account as &#8220;convert.php&#8221; 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<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/\"> Read More ><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4305],"tags":[],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-databases"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Convert a Database to UTF-8 | InMotion Hosting<\/title>\n<meta name=\"description\" content=\"In this tutorial we will show you how to convert a database to a different character set using a script.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Convert a Database to UTF-8 | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"In this tutorial we will show you how to convert a database to a different character set using a script.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2011-08-23T23:16:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-11T15:52:20+00:00\" \/>\n<meta name=\"author\" content=\"Brad Markle\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Brad Markle\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/\"},\"author\":{\"name\":\"Brad Markle\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf\"},\"headline\":\"How to Convert a Database to UTF-8\",\"datePublished\":\"2011-08-23T23:16:11+00:00\",\"dateModified\":\"2022-03-11T15:52:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/\"},\"wordCount\":229,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"articleSection\":[\"Working with Databases\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/\",\"name\":\"How to Convert a Database to UTF-8 | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2011-08-23T23:16:11+00:00\",\"dateModified\":\"2022-03-11T15:52:20+00:00\",\"description\":\"In this tutorial we will show you how to convert a database to a different character set using a script.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Convert a Database to UTF-8\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf\",\"name\":\"Brad Markle\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/bradm\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Convert a Database to UTF-8 | InMotion Hosting","description":"In this tutorial we will show you how to convert a database to a different character set using a script.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/","og_locale":"en_US","og_type":"article","og_title":"How to Convert a Database to UTF-8 | InMotion Hosting","og_description":"In this tutorial we will show you how to convert a database to a different character set using a script.","og_url":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2011-08-23T23:16:11+00:00","article_modified_time":"2022-03-11T15:52:20+00:00","author":"Brad Markle","twitter_card":"summary_large_image","twitter_creator":"@InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"Brad Markle","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/"},"author":{"name":"Brad Markle","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf"},"headline":"How to Convert a Database to UTF-8","datePublished":"2011-08-23T23:16:11+00:00","dateModified":"2022-03-11T15:52:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/"},"wordCount":229,"commentCount":6,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"articleSection":["Working with Databases"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/","url":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/","name":"How to Convert a Database to UTF-8 | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2011-08-23T23:16:11+00:00","dateModified":"2022-03-11T15:52:20+00:00","description":"In this tutorial we will show you how to convert a database to a different character set using a script.","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/how-to-convert-a-database-to-utf-8\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to Convert a Database to UTF-8"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf","name":"Brad Markle","url":"https:\/\/www.inmotionhosting.com\/support\/author\/bradm\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":5,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":106923,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/143\/revisions\/106923"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}