Zen Cart Logo
Forums / Bug Reports / utf-8 and database connection

utf-8 and database connection

Locked

Views: 3,985

Results 1 to 4 of 4
This thread is locked. New replies are disabled.
27 Apr 2008, 04:41
#1
hugo13 avatar

hugo13

Zen Follower

Join Date:
Apr 2004
Location:
vienna
Posts:
176
Plugin Contributions:
4

utf-8 and database connection

in some cases when database and document charset is set to utf-8, the foreign-chars will displayed as question-marks

a soluten could be the following code change in includes\classes\db\mysql\query_factory.php

  function connect($zf_host, $zf_user, $zf_password, $zf_database, $zf_pconnect = 'false', $zp_real = false) {
//@TODO error class required to virtualise & centralise all error reporting/logging/debugging
    $this->database = $zf_database;
    if (!function_exists('mysql_connect')) die ('Call to undefined function: mysql_connect().  Please install the MySQL Connector for PHP');
    if ($zf_pconnect != 'false') {
      $this->link = @mysql_connect($zf_host, $zf_user, $zf_password, true);
    } else {
    // pconnect disabled ... leaving it as "connect" here instead of "pconnect"
      $this->link = @mysql_connect($zf_host, $zf_user, $zf_password, true);
    }
    if ($this->link) {
      if (@mysql_select_db($zf_database, $this->link)) {
        $this->db_connected = true;
        /** set utf-8 database connection
            look at: http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
            &&: http://www.adviesenzo.nl/examples/php_mysql_charset_fix/
            &&: http://de.php.net/manual/en/function.mysql-set-charset.php
        */
        $sql = "SHOW VARIABLES LIKE 'character_set_database'";
        $res = $this->Execute($sql);
        $charset = $res->fields['Value'];
        $sql = "SET NAMES $charset";
        $this->Execute($sql);
        return true;
      } else {
        $this->set_error(mysql_errno(),mysql_error(), $zp_real);
        return false;
      }
    } else {
      $this->set_error(mysql_errno(),mysql_error(), $zp_real);
      return false;
    }
  }
27 Apr 2008, 17:06
#2
jaaa avatar

jaaa

New Zenner

Join Date:
Apr 2008
Posts:
2
Plugin Contributions:
0

Re: utf-8 and database connection

this should be really implemented, Zen Cart is one of the applications which is not able to handle MySQL encoding correctly and this little patch will fully fix it. BTW, i recommend to use "SET CHARACTER SET $charset" instead, it's much better for this.

28 Apr 2008, 04:04
#3
hugo13 avatar

hugo13

Zen Follower

Join Date:
Apr 2004
Location:
vienna
Posts:
176
Plugin Contributions:
4

Re: utf-8 and database connection

why to use SET NAMES
look at: http://de.php.net/manual/en/function.mysql-set-charset.php message from 4.3.2008

Using the function provided by Janez and nag worked fine for SELECTs, but with INSERT and UPDATE the non-ascii characters got converted into question marks. To fix this, I replaced SET CHARACTER SET with SET NAMES (ref: http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html). So the function becomes:

29 Apr 2008, 13:43
#4
jaaa avatar

jaaa

New Zenner

Join Date:
Apr 2008
Posts:
2
Plugin Contributions:
0

Re: utf-8 and database connection

just read the MySQL docs here:
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
and you will understand why to use SET CHARACTER SET instead of SET NAMES (the message from php.net is stupid and not true). i'm useing SET CHARACTER SET many years without problems.