title: UTF-8 character encoding battles json_encode()
body: <p><strong>Quest</strong></p>
  
  <p>I am looking to fetch rows that have accented characters. The encoding for the column (<code>NAME</code>) is <code>latin1_swedish_ci</code>.</p>
  
  <p><strong>The Code</strong></p>
  
  <p>The following query returns <code>Abord â Plouffe</code> using phpMyAdmin:</p>
  
  <pre><code>SELECT C.NAME FROM CITY C
  WHERE C.REGION_ID=10 AND C.NAME_LOWERCASE LIKE '%abor%'
  ORDER BY C.NAME LIMIT 30
  </code></pre>
  
  <p>The following displays expected values (function is called <code>db_fetch_all( $result )</code>):</p>
  
  <pre><code>  while( $row = mysql_fetch_assoc( $result ) ) {
      foreach( $row as $value ) {
        echo $value . " ";
        $value = utf8_encode( $value );
        echo $value . " ";
      }
  
      $r[] = $row;
    }
  </code></pre>
  
  <p>The displayed values: <code>5482 5482 Abord â Plouffe Abord Ã¢ Plouffe</code></p>
  
  <p>The array is then encoded using <code>json_encode</code>:</p>
  
  <pre><code>$rows = db_fetch_all( $result );
  echo json_encode( $rows );
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>The web browser receives the following value:</p>
  
  <pre><code>{"ID":"5482","NAME":null}
  </code></pre>
  
  <p>Instead of:</p>
  
  <pre><code>{"ID":"5482","NAME":"Abord â Plouffe"}
  </code></pre>
  
  <p>(Or the encoded equivalent.)</p>
  
  <p><strong>Question</strong></p>
  
  <p>The documentation states that <code>json_encode()</code> works on UTF-8. I can see the values being encoded from LATIN1 to UTF-8. After the call to <code>json_encode()</code>, however, the value becomes <code>null</code>.</p>
  
  <p>How do I make <code>json_encode()</code> encode the UTF-8 values properly?</p>
  
  <p>One possible solution is to use the <a href="http://www.zfforums.com/zend-framework-general-discussions-1/general-q-zend-framework-2/json-encoding-breaks-strings-containing-special-characters-210.html" rel="noreferrer">Zend Framework</a>, but I'd rather not if it can be avoided.</p>