ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

how to set up mysql_query("SET NAMES 'utf8'"); in codeigniter?

January 20, 2010 1:33pm

Subscribe [4]
  • #16 / Jan 21, 2010 6:04pm

    phpguru_be

    3 posts

    Hi,

    I have been doing research on this as well and found a solution that does not involve fiddlin with the CI standard files.

    Create the following helper in your system/application/helpers folder:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    /**
    * Rewrite all outgoing text into UTF8 compatible streams
    * Author: Matt Carter <[email protected]>
    * Info: <a href="http://hash-bang.net/2009/02/utf8-with-codeigniter">http://hash-bang.net/2009/02/utf8-with-codeigniter</a>
    */
    function ob_utf8($string) {
        return utf8_decode($string);
    }
    ob_start('ob_utf8');
     
    foreach ($_POST as $key => $val) // Re-write all incoming text into UTF8 streams
        $_POST[$key] = utf8_encode($val);
    ?>

    Then simply add it to your system/application/autoload.php:

    $autoload['helper'] = array('utf8');

    The helper will automatically convert incoming POST data into MySQL compatible UTF8 and convert outgoing text into HTML UTF8 streams. No need for

    mysql_query(“SET NAMES ’utf8’”);

    This worked wonders for me, and is now my first step for any new project in CI.
    I hope this helps 😉

    Source: http://hash-bang.net

  • #17 / Jan 21, 2010 6:59pm

    searain

    101 posts

    phpguru_be, thanks for the advice.  I have been using your approach in traditional php not CI.

    Most times, it will work.  In your case, it might be working for all your needs.

    But from my experience, it is not doing everything as the set name ‘utf8’ does.

    First of all, I am not just reading into database from post, I would also read into database from one database to another, from xml or text file (load data file into table) etc.

    Second, it depends on the 3rd party data, sometimes calling php utf8_encode/utf8_decode on these data will not automatically convert them to utf8.  (it is frustrating with these 3rd party data, I am not sure if this set name ‘utf8’ will work on all the cases, but so far, it works.

  • #18 / Jan 21, 2010 7:24pm

    phpguru_be

    3 posts

    Hi,

    Thanks for your reply. I am aware of what you posted, but for some reason I could not get ‘SET NAMES…’ to work in CI… I started Googling and found a few solutions, but the one in my previous post seemed like to less intrusive to me, be it a somewhat slower solution than ‘set names’ as it involves string manipulating, as opposed to getting the irght format back from MYSQL.

    I have an otherwise (read: other than using CI) perfectly good setup for my DB in utf8, and have gotten used to adding set names ‘utf8’ atop any other DB work previously.


    Adding this one line worked for you? Did you change anything else? It did not seem to work for me… though I am not sure why. I get illegible text back for any accented characters. I am from Belgium and the web sites I maintain are built with support for 4 languages…
    Yeah I am not off to a good start with CodeIgniter so far, and if I don4t get this to work quickly then it is quite the showstopper for me.

    Note that in my trial and error, I have been working off the handbook examples, i.e. with the $data array.

  • #19 / Jan 21, 2010 7:35pm

    searain

    101 posts

    What I meant is that add SET NAME in traditional php programming works for me so far.

    But I didn’t try it out with the CI’s solution as it is suggested here.  I would set it up and test it out.  It would take sometime for me to tell if it works for all the cases.  I would see how it works on different 3rd party data I am going to get.

    I will post the results here.

  • #20 / Jan 21, 2010 7:38pm

    phpguru_be

    3 posts

    Oh, I see. We are in fact at the same point exactly 😊
    Good luck, I will post my results back here as well.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases