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

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

[eluser]danmontgomery[/eluser]
[quote author="Demedes" date="1264119521"]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());
[/quote]

Or just disable caching, since that's what this is doing.
#13

[eluser]Sbioko[/eluser]
Yes, of course. But it will be slower.
#14

[eluser]searain[/eluser]
[quote author="Demedes" date="1264124796"]Yes, of course. But it will be slower.[/quote]

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

[eluser]Sbioko[/eluser]
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?
#16

[eluser]phpguru_be[/eluser]
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:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Rewrite all outgoing text into UTF8 compatible streams
* Author: Matt Carter <[email protected]>
* Info: http://hash-bang.net/2009/02/utf8-with-codeigniter
*/
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);
?&gt;

Then simply add it to your system/application/autoload.php:
Code:
$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
Quote: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 Wink

Source: http://hash-bang.net
#17

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

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

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

[eluser]phpguru_be[/eluser]
Oh, I see. We are in fact at the same point exactly Smile
Good luck, I will post my results back here as well.




Theme © iAndrew 2016 - Forum software by © MyBB