Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

2.4 and mysql utf-8


Go to End


5 Posts   4393 Views

Avatar
Kalileo

Community Member, 127 Posts

29 March 2010 at 3:08pm

Edited: 29/03/2010 4:23pm

I remember having seen a statement that the mysql connection in SS 2.4 would default to utf-8, but I cannot find that statement anymore. It might have been in a bug ticket about utf-8. So my question is, how about the default for 2.4, will mysql connections default to utf-8?

If it's meant to be like that, then it didn't work in my tests. The code in MySQLDatabase.php Line 43


public static function set_connection_charset($charset = 'utf8') {
	self::$connection_charset = $charset;
}

and line 59:


if(self::$connection_charset) {
	$this->query("SET CHARACTER SET '" . self::$connection_charset . "'"); 
	$this->query("SET NAMES '" . self::$connection_charset . "'");
}

did not result in setting the connection to UTF-8. Only adding there

	$this->query("SET CHARACTER SET 'utf8'"); 
	$this->query("SET NAMES 'utf8'");

did it. But that would be hacking the core. I think the current code had flexibility in mind, so let me propose a solution which is flexible but still enforces UTF-8 (or whatever I want) if I want to. Simply add in mysite/_config.php one setting encoding in $databaseConfig and have MySQLDatabase.php use it (if present):


$databaseConfig = array(
	"type" => "MySQLDatabase",
	"server" => "localhost", 
	"username" => "xxxxxxxx", 
	"password" => 'xxxxxxxx', 
	"database" => "xxxxxxxx",
	"encoding" => "utf8",
);

Another solution would be to add Malte's UTF8MySQLDatabase class, good enough for me, but that would not give the flexibility to force any encoding.

Avatar
Willr

Forum Moderator, 5523 Posts

29 March 2010 at 4:25pm

How its designed to work is the following line is included in your mysite/_config file when you install so yes this is the 'default'.

MySQLDatabase::set_connection_charset('utf8');

Avatar
Kalileo

Community Member, 127 Posts

29 March 2010 at 5:37pm

Thanks Will, I'll add that line.

My tests were 2.4 alpha installs upgraded to 2.4 beta, without overwriting the mysite/_config, so I assume you added that line between 2.4 alpha and beta.

Anyway, a good solution.

Avatar
pinkp

Community Member, 182 Posts

17 June 2010 at 12:09pm

Edited: 17/06/2010 12:35pm

I am getting this error does it matter, what does it mean?

Error: Couldn't run query: SET CHARACTER SET 'utf8'
At line 536 in /home/sites/mysite.co.uk/public_html/sapphire/core/model/MySQLDatabase.php

user_error(Couldn't run query: SET CHARACTER SET 'utf8' ,256) line 536 of MySQLDatabase.php MySQLDatabase->databaseError(Couldn't run query: SET CHARACTER SET 'utf8' | ,256) line 134 of MySQLDatabase.php MySQLDatabase->query(SET CHARACTER SET 'utf8') line 60 of MySQLDatabase.php MySQLDatabase->__construct(Array) line 94 of DB.php DB::connect(Array) line 121 of main.php

EDIT:

and this:

Error: Couldn't run query: SET CHARACTER SET 'utf8'
At line 536 in /home/sites/mysite.co.uk/public_html/sapphire/core/model/MySQLDatabase.php

Couldn't run query: SET CHARACTER SET 'utf8'
Line 536 of MySQLDatabase.php
MySQLDatabase->databaseError(Couldn't run query: SET CHARACTER SET 'utf8' | ,256)
Line 134 of MySQLDatabase.php
MySQLDatabase->query(SET CHARACTER SET 'utf8')
Line 60 of MySQLDatabase.php
MySQLDatabase->__construct(Array)
Line 94 of DB.php
DB::connect(Array)
Line 121 of main.php

EDIT SOLVED: Changed the "server" in the _config.php by mistake!

Avatar
geekdenz

Community Member, 37 Posts

20 August 2014 at 12:52pm

I encountered a problem with the PHP function substr. I just used mb_substr instead and it solved my issue.