title: How do I make MySQL return UTF-8?
body: <p>I'm using PHPUnit to validate XML output from my PHP code, but apparently I have problems with the character encoding <a href="http://en.wikipedia.org/wiki/MySQL" rel="noreferrer">MySQL</a> returns. Here is the error I get from DOMDocument:</p>
  
  <pre><code>Input is not proper UTF-8, indicate encoding!
  Bytes: 0xE9 0x20 0x42 0x65
  </code></pre>
  
  <p>I initialize the DOMDocument so it uses the correct encoding:</p>
  
  <pre><code>$domDocument = new DOMDocument('1.0','UTF-8');
  </code></pre>
  
  <p>And when I check the output from saveXML() using mb_detect_encoding the result is <a href="http://en.wikipedia.org/wiki/UTF-8" rel="noreferrer">UTF-8</a>.</p>
  
  <p>I also checked all the calls used to create the XML, using mb_detect_encoding on all createCDATASection parameters encountered and they are all either UTF-8 or ASCII (there are no plain text nodes, everything is in <a href="https://en.wikipedia.org/wiki/CDATA" rel="noreferrer">CDATA</a> blocks).</p>
  
  <p>I think the issue comes from the use of an 'é' character (which is 0xE9 in <a href="https://en.wikipedia.org/wiki/ISO/IEC_8859-1" rel="noreferrer">ISO 8859-1</a>). The line which adds that character to my XML is:</p>
  
  <pre><code>$domDocument-&gt;createCDATASection($place-&gt;name);
  </code></pre>
  
  <p>and mb_detect_encoding($place->name) gives me UTF-8.</p>
  
  <p>The data ($place->name) is pulled from a MySQL database. This database has the UTF-8 charset.</p>
  
  <p>Here is some example code:</p>
  
  <pre><code>$query = sprintf('SELECT name FROM place where id = 1');
  $result = mysql_query($query);
  $result = mysql_fetch_assoc($result);
  
  
  // -- Feeding UTF-8 data directly WORKS
  $domDocument = new DOMDocument('1.0','UTF-8');
  $rootNode = $domDocument-&gt;createElement('Response');
  $rootNode-&gt;appendChild($domDocument-&gt;createCDATASection('Café Belga'));
  $domDocument-&gt;appendChild($rootNode);
  
  $matcher = array('tag' =&gt; 'Response');
  self::assertTag($matcher, $domDocument-&gt;saveXML(), '', FALSE);
  
  // -- Feeding UTF-8 data from the resultset FAILS
  $domDocument = new DOMDocument('1.0','UTF-8');
  $rootNode = $domDocument-&gt;createElement('Response');
  $rootNode-&gt;appendChild($domDocument-&gt;createCDATASection($result['name']));
  $domDocument-&gt;appendChild($rootNode);
  
  $matcher = array('tag' =&gt; 'Response');
  self::assertTag($matcher, $domDocument-&gt;saveXML(), '', FALSE);
  </code></pre>
  
  <p>In my PHPStorm debugger, the string fetched from the database looks like this:</p>
  
  <blockquote>
    <p>Caf� Belga</p>
  </blockquote>
  
  <p>So I think that is the root of the problem. In MySQLWorkbench the string is correct: Café Belga.</p>
  
  <p>When using <code>utf8_encode($result['name'])</code>, however, everything works fine!</p>
  
  <p>One more check in the watches window:</p>
  
  <p><code>mb_detect_encoding($result['name'])</code> -> "UTF-8"</p>
  
  <p><code>mb_detect_encoding(utf8_encode($result['name']))</code> -> "UTF-8"</p>
  
  <p>On a side note, are there any sites where I can simply copy-paste those hex values and see what characters they are supposed to be in different character sets?</p>