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]
  • #1 / Jan 20, 2010 1:33pm

    searain

    101 posts

    Hello,

    I used to run mysql_query(“SET NAMES ‘utf8’”); after the database connection.  This is my general set up, make sure the characters are utf8 after the mysql query.

    Where and how could I set up this in codeigniter configuration? So I don’t have to make this query call before each mysql query?

    Or the config/database.php

    $db['default']['char_set'] = "utf8";
    $db['default']['dbcollat'] = "utf8_general_ci";

    Already did it for me.  That I don’t need to call mysql_query(“SET NAMES ‘utf8’”) any more?

    Thanks!

  • #2 / Jan 20, 2010 3:22pm

    Blux

    4 posts

    i think you still need it ...
    i have encountered the same problem some time a go with DX_Auth .. and i solved it with the SET NAMES ‘utf8’
    even though i used

    $db['default']['char_set'] = "utf8";
    $db['default']['dbcollat'] = "utf8_general_ci";

    now, i’m having the exact same problem now, and trying to re-solve it XD
    i don’t know why it’s not working anymore ! =\

    just don’t forget to use

    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

    in the view ..

  • #3 / Jan 20, 2010 4:55pm

    Sbioko

    382 posts

    You can run this mysql query in the system/database/DB.php file at the end of DB function.

    $DB->simple_query('SET NAMES \'utf8\'');
  • #4 / Jan 20, 2010 6:04pm

    searain

    101 posts

    You mean in system/database/DB.php

    if ($DB->autoinit == TRUE)
        {
            $DB->initialize();
        }
        
        $DB->simple_query('SET NAMES \'utf8\''); //just add this line here, and that is all I have to do?
    
        return $DB;

    This will require us to change the file outside the application folder.  Would that be a problem in the future when we update the codeigniter (we can document these changes for the future update if have to)?  Any other ways that we can do the same such as add/edit some files in the application folder, so without worrying about codeigniter update?

    Or I don’t have to worry about it in the first place.

    Thanks!

  • #5 / Jan 21, 2010 1:00am

    Sbioko

    382 posts

    Oh, come on, who knows when will be released the new CI? This is one line of code, this will not be a problem to paste this in the new CI. You can freely do it. You need to paste my code before this:

    return $DB;

    You are welcome!

  • #6 / Jan 21, 2010 1:02am

    Sbioko

    382 posts

    For my CMS I changed all system files. Absolutely all. And I’m not scared of what you said before.

  • #7 / Jan 21, 2010 5:02am

    n0xie

    1381 posts

    For my CMS I changed all system files. Absolutely all. And I’m not scared of what you said before.

    Luckily you’re the only one.

  • #8 / Jan 21, 2010 10:16am

    Sbioko

    382 posts

    Can you post more then one argument, that will show that I’m wrong? Forward compability is the first one. Right?

  • #9 / Jan 21, 2010 2:11pm

    searain

    101 posts

    First of all.

    Thanks for this solution.  If that is the only simple way to set up this, I would use this approach.  Just add one line and I will keep a clear document, specially for the files modified outside the system files

    But due to we may work in a team, and other team members may not expect the changes in the system files, or by the same token, I would not expect them to change in the system files.  Specially if the previous programmer is not in house any more etc.  I would prefer a change in application folder.  Could we create a subclass just extent the system class and add this line in the subclass?

    Any one already did that? 

    I hope CI new version will have this feature built in.

  • #10 / Jan 21, 2010 2:18pm

    Sbioko

    382 posts

    Ok, there is another solution, if you don’t want to change system files. It is normal. You need just put this code into your constructor of controller:

    $this->db->simple_query('SET NAMES \'utf-8\'');

    And it will do this operation every time, when your controller executes. BUT, there is one small problem. Don’t forget about database caching. If it turned on, this mysql query will be done only 1 time. So, before running this query you must do this:

    $this->db->cache_delete($this->router->fetch_class(), $this->router->fetch_method());

    So, here is a complete code:

    <?php
    class Example extends Controller {
        
        function Example()
        {
            parent::Controller();
            $this->db->cache_delete($this->router->fetch_class(), $this->router->fetch_method());
            $this->db->simple_query('SET NAMES \'utf-8\'');
        }
    
        // Your methods
        
    }

    Waiting for your response 😊

  • #11 / Jan 21, 2010 2:27pm

    searain

    101 posts

    So instead of add these two lines in each controllers.

    Should I just extends the controller in general, make a MY_Controller?  And add these two lines in MY_Controller and be done with it?

  • #12 / Jan 21, 2010 2:58pm

    danmontgomery

    1802 posts

    BUT, there is one small problem. Don’t forget about database caching. If it turned on, this mysql query will be done only 1 time. So, before running this query you must do this:

    $this->db->cache_delete($this->router->fetch_class(), $this->router->fetch_method());

    Or just disable caching, since that’s what this is doing.

  • #13 / Jan 21, 2010 3:46pm

    Sbioko

    382 posts

    Yes, of course. But it will be slower.

  • #14 / Jan 21, 2010 4:04pm

    searain

    101 posts

    Yes, of course. But it will be slower.

    You mean use MY_Controller will be slower?  It shouldn’t too slow otherwise it beats up the approach of using sub class.  But I didn’t test it out, so I really don’t know.

    How much will it be slower in your experience in the same kind cases?

    Thanks!

  • #15 / Jan 21, 2010 4:22pm

    Sbioko

    382 posts

    Yes. It will be slower, because you forgot about memory management. Loading new and new php files will increasing memory, and as a consequence, cpu load. Of course, for small projects it will not be slower, but if you are planning(maybe in future) to make a big project, definitely use my method. It is not critically, to put 2 lines of code to the start of your controller. Am I wrong?

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

ExpressionEngine News!

#eecms, #events, #releases