Community Add-Ons

Maintainers: draculakos
Add file to this package
Top » Other

Corrupted character on mysql with utf-8
for osCommerce Online Merchant v2.2

I am using the osCommerce RC2 with Greek language utf-8.. While i was exploring in the mysql i saw that the whole language were corrupted.. In the site it was looking good, but in the mysql were TOTALLY corrupted and that a problem..

After all i found a solution..

### open includesfunctionsdatabase.php ####
And find this :

function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link') {
global $$link;

if (USE_PCONNECT == 'true') {
$$link = mysql_pconnect($server, $username, $password);
} else {
$$link = mysql_connect($server, $username, $password);
}

if ($$link) mysql_select_db($database);

return $$link;
}


#### And replace with this ####

function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link') {
global $$link;

if (USE_PCONNECT == 'true') {
$$link = mysql_pconnect($server, $username, $password);
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");

} else {
$$link = mysql_connect($server, $username, $password);
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");

}

if ($$link) {
mysql_select_db($database);
tep_db_query('SET time_zone='' . CONFIG_TIME_ZONE . ''');
}

return $$link;
}



Do the same for the adminincludesfunctionsdatabase.php

I hope this helps some people who may have the same problems as me..

Onoufriadis Christoforos
http://www.turbosim.gr
17 July 2008

Legend:  Download   Report
Expand All / Collapse All
SET NAMES should suffice IndiaStarker 7 Jul 2009  

Changes:
I have removed the SET CHARACTER SET line and kept the SET NAMES line and moved it
outside of the if clauses for simplicity.

In catalog/includes/functions/database.php and catalog/admin/includes/functions/database.php,
in the function tep_db_connect(), add the lines
// Added for oscommerce utf-8
mysql_query("SET NAMES 'utf8'");

before the line

return $$link;

Entire function:

function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link') {
global $$link;

if (USE_PCONNECT == 'true') {
$$link = mysql_pconnect($server, $username, $password);
} else {
$$link = mysql_connect($server, $username, $password);
}

if ($$link) mysql_select_db($database);

// Added for oscommerce utf-8
mysql_query("SET NAMES 'utf8'");

return $$link;
}

Why I changed it:
It would appear that one should use either one or the other but not both.
Moreover, SET CHARACTER SET is similar to SET NAMES, but sets the connection character set
and collation to be those of the default database rather than the one you specify
which is probably *not* what you want. The way the original (below) was written,
the SET NAMES being second perhaps overrides the other.
I placed the charset in single quotes as shown on the dev.mysql.com site.


Sources :
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html http://forum.alsacreations.com/topic-17-40579-1-resolu-Chinois-et-base-de-donnees.html
http://wiki.pooteeweet.org/MDB2/CharacterSet

Corrupted character on mysql with utf-8 draculakos 16 Jul 2008