title: Codeigniter and charsets
body: <p>I'm using Codeigniter not for so long but I've some charset problems.. I'm asking around at the CI Forum, but I want to go further, still no global solution: <a href="http://codeigniter.com/forums/viewthread/204409/" rel="nofollow noreferrer">http://codeigniter.com/forums/viewthread/204409/</a></p>
  
  <p>The problem was a database error 1064. I've got a solution, use iconv! Works fine, but I think it's not necessary. I'm searching a lot on the internet for charset's etc but I'm using CI now, how about charsets and CI...</p>
  
  <p>So I've a lot of question about it, I hope someone can make it clear for me:</p>
  
  <p>What’s the best way to set the charset global? And what to set?</p>
  
  <ul>
  <li><p>In the head</p>
  
  <p><code>&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;</code></p></li>
  <li><p>In config/config.php</p>
  
  <p><code>$config['charset'] = 'UTF-8';</code></p></li>
  <li><p>In config/database.php</p>
  
  <p><code>$db['default']['char_set'] = 'utf8';</code></p>
  
  <p><code>$db['default']['dbcollat'] = 'utf8_general_ci';</code></p></li>
  <li><p>In .htaccess, my rewrite rules and</p>
  
  <p><code>php_value magic_quotes_gpc Off</code></p>
  
  <p><code>AddDefaultCharset UTF-8</code></p></li>
  <li><p>Also need send a header? Where to place? Something like?</p>
  
  <p><code>header('Content-Type: text/html; charset=UTF-8');</code></p></li>
  <li><p>In my editor (Notepad++) save files as UTF-8? Or UTF-8 (without BOM)? Or is ANSI good (this is what I’m using now)?</p></li>
  <li><p>Use utf8_unicode_ci or utf8_general_ci for the MySQL database? And why?</p></li>
  <li><p>How about reading RSS feeds, how to handle multiple charsets? Where I’m working on I’ve two feeds, one with UTF-8 encoding and the other with ISO-8859-1. This will be stored in the database and will be compared sometimes to see if there are new items. It fails on special chars.</p></li>
  </ul>
  
  <p>I'm working with:
  - CI 2.0.3
  - PHP 5.2.17
  - MySQL 5.1.58</p>
  
  <p><strong>More information added:</strong></p>
  
  <p>Model:</p>
  
  <pre><code>function update_favorite($data) 
  {
   $this-&gt;db-&gt;where('id', $data['id']);
   $this-&gt;db-&gt;where('user_id', $data['user_id']);
   $this-&gt;db-&gt;update('favorites', $data);
   return;
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>$this-&gt;favorites_model-&gt;update_favorite(array(
   'id' =&gt; $id, 
   'rss_last' =&gt; $rss_last,
   'user_id' =&gt; $this-&gt;session-&gt;userdata('user_id')
  )); 
  </code></pre>
  
  <p>When $rss_last is a “normal” value like: “test” (without quotes) it works fine.
  When it’s a value with more length like (in Dutch): F-Secure vindt malware met certificaat van Maleisische overheid</p>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>Error Number: 1064</p>
    
    <p>You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use
    near ‘vindt malware met certificaat van Maleisische overheid,
    <code>user_id</code> = ‘1’ WHERE `i’ at line 1</p>
    
    <p>UPDATE <code>favorites</code> SET <code>id</code> = ‘15’, <code>rss_last</code> = F-Secure vindt
    malware met certificaat van Maleisische overheid, <code>user_id</code> = ‘1’
    WHERE <code>id</code> = ‘15’ AND <code>user_id</code> = ‘1’</p>
    
    <p>Filename:
    /home/.../domains/....nl/public_html/new/models/favorites_model.php</p>
    
    <p>Line Number: 35</p>
  </blockquote>
  
  <p>Someone at the CI forum told me to use this:</p>
  
  <pre><code>'rss_last' =&gt; iconv("UTF-8", "UTF-8//TRANSLIT", $rss_last) 
  </code></pre>
  
  <p>This works fine, but I think this is not necessary..</p>
  
  <p>The value $rss_last came out a RSS feed, as told before, sometimes a UTF-8 and other times a ISO-8859-1 encoding:</p>
  
  <pre><code>$rss = file_get_contents('http://www.website.com/rss.xml');
  $feed = new SimpleXmlElement($rss);
  $rss_last = $feed-&gt;channel-&gt;item[0]-&gt;title;
  </code></pre>
  
  <p>It looks like this last part is the problem, when $rss_last is set to the value it works fine:</p>
  
  <pre><code>$rss_last = 'F-Secure vindt malware met certificaat van Maleisische overheid';
  </code></pre>
  
  <p>When the value came out the RSS it give problems...</p>
  
  <p><strong>Some more questions..</strong></p>
  
  <p>Just found this: <a href="https://stackoverflow.com/questions/910793/php-detect-encoding-and-make-everything-utf-8">Detect encoding and make everything UTF-8</a></p>
  
  <p>Best solution? But.. is iconv not more simple, do something like this:</p>
  
  <pre><code>$encoding = some_function_to_get_encoding_from_feed($feed);
  $rss_last = iconv($encoding, "UTF-8//TRANSLIT", $feed-&gt;channel-&gt;item[0]-&gt;title);
  </code></pre>
  
  <p>But what to use for "some_function_to_get_encoding_from_feed"? mb_detect_encoding?</p>
  
  <p>And mb_convert_encoding vs iconv?</p>