Welcome Guest, Not a member yet? Register   Sign In
how to set up mysql_query("SET NAMES 'utf8'"); in codeigniter?
#1

[eluser]searain[/eluser]
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
Code:
$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

[eluser]Blux[/eluser]
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
Code:
$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
Code:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
in the view ..
#3

[eluser]Sbioko[/eluser]
You can run this mysql query in the system/database/DB.php file at the end of DB function.
Code:
$DB->simple_query('SET NAMES \'utf8\'');
#4

[eluser]searain[/eluser]
You mean in system/database/DB.php
Code:
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

[eluser]Sbioko[/eluser]
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:
Code:
return $DB;
You are welcome!
#6

[eluser]Sbioko[/eluser]
For my CMS I changed all system files. Absolutely all. And I'm not scared of what you said before.
#7

[eluser]n0xie[/eluser]
[quote author="Demedes" date="1264071725"]For my CMS I changed all system files. Absolutely all. And I'm not scared of what you said before.[/quote]
Luckily you're the only one.
#8

[eluser]Sbioko[/eluser]
Can you post more then one argument, that will show that I'm wrong? Forward compability is the first one. Right?
#9

[eluser]searain[/eluser]
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

[eluser]Sbioko[/eluser]
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:
Code:
$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:
Code:
$this->db->cache_delete($this->router->fetch_class(), $this->router->fetch_method());

So, here is a complete code:
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 :-)




Theme © iAndrew 2016 - Forum software by © MyBB