Google Groups Home
Help | Sign in
The MySQL UTF8 Issue
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  19 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
bentryster@gmail.com  
View profile
 More options Apr 10 2006, 10:29 am
From: "bentrys...@gmail.com" <bentrys...@gmail.com>
Date: Mon, 10 Apr 2006 07:29:07 -0700
Local: Mon, Apr 10 2006 10:29 am
Subject: The MySQL UTF8 Issue
    I use mysql5 and utf8 table to store Chinese data. Everything
insert into the utf8 tables though cake can be fetch out by cake fine.
But when I use other database tools (such as MySQL Query Browser) to
query the data which insert by cake will got unreadable characters. I'm
so confuse on it, and serach on the internet. I think there is
something wrong with the mysql connections setting in cake.
    In my controller  I query the MySQL connection setting by  do this

...
$msg = $this->MyModel->findBySql("Show VARIABLES LIKE
'%CHARACTER_SET%';");
// Then set the result varable $msg to display in views
$this->set('msg',$msg);
....

In view:
...
debug($msg);
...

Then I got this result:
Array
(
    [0] => Array
        (
            [VARIABLES] => Array
                (
                    [Variable_name] => character_set_client
                    [Value] => latin1
                )

        )

    [1] => Array
        (
            [VARIABLES] => Array
                (
                    [Variable_name] => character_set_connection
                    [Value] => latin1
                )

        )

    [2] => Array
        (
            [VARIABLES] => Array
                (
                    [Variable_name] => character_set_database
                    [Value] => utf8
                )

        )

    [3] => Array
        (
            [VARIABLES] => Array
                (
                    [Variable_name] => character_set_results
                    [Value] => latin1
                )

        )

    [4] => Array
        (
            [VARIABLES] => Array
                (
                    [Variable_name] => character_set_server
                    [Value] => utf8
                )

        )

    [5] => Array
        (
            [VARIABLES] => Array
                (
                    [Variable_name] => character_set_system
                    [Value] => utf8
                )

        )

    [6] => Array
        (
            [VARIABLES] => Array
                (
                    [Variable_name] => character_sets_dir
                    [Value] => D:\Program Files\MySQL\MySQL Server
5.0\share\charsets\
                )

        )

)

The character_set_client,character_set_results,character_set_connection
is Laten1, and the other is utf8. I think the problem is here. So I
continue the test:

When I query the data in the utf8 table by convert the field to laten1
encoding, I can get the right chinese string. The SQL is:
SELECT CONVERT(myField USING utf8) from myUtf8Table;

How can I change the database connection setting to solve this problem?
Should I modify the core code?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RosSoft  
View profile
 More options Apr 10 2006, 4:30 pm
From: "RosSoft" <ross...@gmail.com>
Date: Mon, 10 Apr 2006 20:30:23 -0000
Local: Mon, Apr 10 2006 4:30 pm
Subject: Re: The MySQL UTF8 Issue
    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mihailo Stefanovic  
View profile
(2 users)  More options Apr 10 2006, 8:05 pm
From: "Mihailo Stefanovic" <mstefano...@gmail.com>
Date: Tue, 11 Apr 2006 02:05:08 +0200
Local: Mon, Apr 10 2006 8:05 pm
Subject: Re: The MySQL UTF8 Issue
I had the same problem (with Cyrillic letters), and only/most elegant
solution I found was to modify the core.

Try this: in file "cake/libs/model/dbo/dbo_mysql.php" find function
connect(). Replace these lines:

        if (mysql_select_db($config['database'], $this->connection))
        {
            $this->connected = true;
        }

with this:

        if (mysql_select_db($config['database'], $this->connection))
        {
            mysql_query("SET names utf8");
            $this->connected = true;
        }

I wish there was more elegant solution, without modifying the core.
Perhaps another setting in DATABASE_CONFIG (app/config/database.php),
something like:

    var $default = array('driver'    => 'mysql',
                         'connect'  => 'mysql_connect',
                         'host'     => 'localhost',
                         'login'    => 'user',
                         'password' => 'password',
                         'database' => 'project_name',
                         'charset' => 'utf8',
                         'prefix'    => '');


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mika  
View profile
 More options Apr 11 2006, 12:48 am
From: "Mika" <mmihajlo...@gmail.com>
Date: Mon, 10 Apr 2006 21:48:57 -0700
Local: Tues, Apr 11 2006 12:48 am
Subject: Re: The MySQL UTF8 Issue
I think this is a good idea. Why not make a ticket for it in trac?
(https://trac.cakephp.org/)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bentryster@gmail.com  
View profile
 More options Apr 11 2006, 3:02 am
From: "bentrys...@gmail.com" <bentrys...@gmail.com>
Date: Tue, 11 Apr 2006 00:02:24 -0700
Local: Tues, Apr 11 2006 3:02 am
Subject: Re: The MySQL UTF8 Issue
Thanks for everyone. I have made the ticket for this problem.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andreas Waidelich  
View profile
 More options Apr 11 2006, 4:20 am
From: "Andreas Waidelich" <andreas.waidel...@gmail.com>
Date: Tue, 11 Apr 2006 10:20:34 +0200
Local: Tues, Apr 11 2006 4:20 am
Subject: Re: The MySQL UTF8 Issue
I used my AppModel to solve the issue.

function __construct()
        {
                $this->execute("SET NAMES 'utf8'");
                parent::__construct();

        }

2006/4/11, bentrys...@gmail.com <bentrys...@gmail.com>:

> Thanks for everyone. I have made the ticket for this problem.

--

e> andreas.waidel...@gmail.com
w> http://andreas-waidelich.de


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bentryster@gmail.com  
View profile
 More options Apr 11 2006, 10:06 am
From: "bentrys...@gmail.com" <bentrys...@gmail.com>
Date: Tue, 11 Apr 2006 07:06:18 -0700
Local: Tues, Apr 11 2006 10:06 am
Subject: Re: The MySQL UTF8 Issue
Andreas:
    Is any Model should add  $this->execute("SET NAMES 'utf8'"); ?
   Or only one model is enough?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andreas Waidelich  
View profile
 More options Apr 11 2006, 10:24 am
From: "Andreas Waidelich" <andreas.waidel...@gmail.com>
Date: Tue, 11 Apr 2006 16:24:19 +0200
Local: Tues, Apr 11 2006 10:24 am
Subject: Re: The MySQL UTF8 Issue
You just have to do it once in your AppModel (located in your
application's root directory).

my AppModel looks like this:

class AppModel extends Model
{
  function __construct()
  {
    $this->execute("SET NAMES 'UTF8');
    parent::__construct();
  }

}

2006/4/11, bentrys...@gmail.com <bentrys...@gmail.com>:

> Andreas:
>     Is any Model should add  $this->execute("SET NAMES 'utf8'"); ?
>    Or only one model is enough?

--

e> andreas.waidel...@gmail.com
w> http://andreas-waidelich.de


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bentryster@gmail.com  
View profile
 More options Apr 11 2006, 10:31 am
From: "bentrys...@gmail.com" <bentrys...@gmail.com>
Date: Tue, 11 Apr 2006 07:31:12 -0700
Local: Tues, Apr 11 2006 10:31 am
Subject: Re: The MySQL UTF8 Issue
Andreas:
When I add the __construct() function into my model. I got this error:

Fatal error: Call to a member function fetchAll() on a non-object in
D:\php-5.0.4-Win32\PHP_Script\PHP_CAKE\cake\libs\model\model_php5.php
on line 1143

I don't know why.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andreas Waidelich  
View profile
 More options Apr 11 2006, 10:50 am
From: "Andreas Waidelich" <andreas.waidel...@gmail.com>
Date: Tue, 11 Apr 2006 16:50:51 +0200
Local: Tues, Apr 11 2006 10:50 am
Subject: Re: The MySQL UTF8 Issue
Can you paste your code, please.

2006/4/11, bentrys...@gmail.com <bentrys...@gmail.com>:

> Andreas:
> When I add the __construct() function into my model. I got this error:

> Fatal error: Call to a member function fetchAll() on a non-object in
> D:\php-5.0.4-Win32\PHP_Script\PHP_CAKE\cake\libs\model\model_php5.php
> on line 1143

> I don't know why.

--

e> andreas.waidel...@gmail.com
w> http://andreas-waidelich.de
m> +49.177.423 09 48


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RosSoft  
View profile
 More options Apr 11 2006, 1:02 pm
From: "RosSoft" <ross...@gmail.com>
Date: Tue, 11 Apr 2006 17:02:38 -0000
Local: Tues, Apr 11 2006 1:02 pm
Subject: Re: The MySQL UTF8 Issue
If you overwrite the model constructor, then you have to call parent's
one

Have you done parent::__construct () ?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bentryster@gmail.com  
View profile
 More options Apr 12 2006, 8:28 pm
From: "bentrys...@gmail.com" <bentrys...@gmail.com>
Date: Wed, 12 Apr 2006 17:28:59 -0700
Local: Wed, Apr 12 2006 8:28 pm
Subject: Re: The MySQL UTF8 Issue
 Thanks Andreas  , I add the code in the model int the model directory,
so it can't work.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bentryster@gmail.com  
View profile
 More options Apr 15 2006, 10:28 am
From: "bentrys...@gmail.com" <bentrys...@gmail.com>
Date: Sat, 15 Apr 2006 07:28:52 -0700
Local: Sat, Apr 15 2006 10:28 am
Subject: Re: The MySQL UTF8 Issue