{"id":723,"date":"2013-04-10T14:33:38","date_gmt":"2013-04-10T13:33:38","guid":{"rendered":"http:\/\/www.ericlee.info\/blog\/?p=723"},"modified":"2013-04-10T14:33:38","modified_gmt":"2013-04-10T13:33:38","slug":"unicode-drupal-php-mysql-a-love-story-not","status":"publish","type":"post","link":"https:\/\/www.ericlee.info\/blog\/unicode-drupal-php-mysql-a-love-story-not\/","title":{"rendered":"Unicode, Drupal, PHP, MySQL: A love story (not)"},"content":{"rendered":"<p>I have been trying to do something fairly simple: I wanted to write a web page that could render well on a small screen (like a mobile phone) and which would display content from the <a href=\"http:\/\/www.iuf.org\">IUF website<\/a>. \u00a0How hard could this be?<\/p>\n<p>Well, it shouldn&#8217;t have been hard.<\/p>\n<p>The content on the IUF site, which is handled by a content management system called Drupal, is stored in a MySQL database. \u00a0The characters are encoded as Unicode (UTF-8) &#8212; because that&#8217;s what you need to do when you have multilingual content on your site, and the IUF site works in Arabic, Chinese, Russian and many other non-Latin languages.<\/p>\n<p>So it should have been fairly simple to write a few lines of code in the widely-used PHP programming language to read news headlines from the IUF&#8217;s database and show them on screen.<\/p>\n<p>Except that it was showing gibberish every time there was an accented character.<\/p>\n<p>I posted a message calling for help on the Drupal forums. \u00a0(Not a big response there.) \u00a0I wrote to three very smart friends who understand these things and they all had good ideas, and pointed me in different directions, but to no avail. \u00a0Nothing was working. \u00a0One suggested that I give up. \u00a0I nearly did give up.<\/p>\n<p>And then I decided to search again, and found <a href=\"http:\/\/www.webmasterworld.com\/php\/3553642.htm\">this page<\/a> where a programmer named &#8220;HoboTraveler&#8221; (I&#8217;m guessing that&#8217;s not his real name) encountered a similar problem on January 21st, 2008, more than five years ago. \u00a0He writes asking for help, gets loads of tips, tries a million things, nearly gives up, and then, dozens of comments later, he solves the problem.<\/p>\n<p>He writes:<\/p>\n<blockquote><p>I think I got it to work with multiple databases!<\/p>\n<p>I&#8217;ve inserted the line:<\/p>\n<p>mysql_query(&#8216;SET NAMES utf8&#8217;);<\/p>\n<p>just after the mysql_connect() and it seems to work.<\/p><\/blockquote>\n<p>So I thought, what the hell, I&#8217;ll try that. \u00a0I tried everything else. \u00a0And &#8220;HoboTraveler&#8221;, whoever you are, you made my day.<\/p>\n<p>Lesson of the story: sometimes life is hard. \u00a0And thank God for Google.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been trying to do something fairly simple: I wanted to write a web page that could render well on a small screen (like a mobile phone) and which would display content from the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-723","post","type-post","status-publish","format-standard","hentry","category-web-exclusive"],"_links":{"self":[{"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/posts\/723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/comments?post=723"}],"version-history":[{"count":1,"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/posts\/723\/revisions"}],"predecessor-version":[{"id":724,"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/posts\/723\/revisions\/724"}],"wp:attachment":[{"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/media?parent=723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/categories?post=723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ericlee.info\/blog\/wp-json\/wp\/v2\/tags?post=723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}