We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Special characters import in MySQL

Hello,

Im importing XML data in to the MySQL database. Im parsing it with XMLSimple php, but special characters not showing in database (looks like symobls). All tables in MySQL are allready UTF 8 general ci. Does anyone know a good way to fix import ?

Thx

What do you mean they don't show in DB? Are they completely missing, or showing up as HTMl entities (like & or &)? Also, how do they look like in the source XML file?



23.6k

What do you mean they don't show in DB? Are they completely missing, or showing up as HTMl entities (like & or &)? Also, how do they look like in the source XML file?

They are showing like "Köp , pÃ¥". And specail characters looks fine on XML (ä, é, ö).

edited Mar '16

If using the DB sevice provided by Phalcon, do you have your services setup like this?

$di->set('db', function() use ($config) {
    return new DbAdapter(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname,
        'charset' => 'utf8', // important field
    ));
});

If using native PHP DB methods, issue this query at the beginning: SET NAMES utf8;



23.6k
edited Mar '16

If using the DB sevice provided by Phalcon, do you have your services setup like this?

$di->set('db', function() use ($config) {
   return new DbAdapter(array(
       'host' => $config->database->host,
       'username' => $config->database->username,
       'password' => $config->database->password,
       'dbname' => $config->database->dbname,
       'charset' => 'utf8', // important field
   ));
});

If using native PHP DB methods, issue this query at the beginning: SET NAMES utf8;

I have just added that line in my config but still imporing witouth special characters .. Looks like charset when importing dosent wordk ..

    $configuration = new Config(array(
    'debug' => true,
    'salt' => 'somecodehere',
    'db_development' => array(
        'adapter' => 'mysql',
        'host' => 'localhost',
        'username' => 'someusername',
        'password' => 'somepassword',
        'dbname' => 'somename',
        'charset' => 'utf8',
    ),

To be honest, this is not a problem with Phalcon. Maybe your XML document has a different encoding specified in the root tag than the actual encoding of the file itself. Check the raw output of a SimpleXML element before passing it to the DB.



23.6k
edited Mar '16

To be honest, this is not a problem with Phalcon. Maybe your XML document has a different encoding specified in the root tag than the actual encoding of the file itself. Check the raw output of a SimpleXML element before passing it to the DB.

I have just made a simple external php script to test parsing my XML and just echo it and it works well ... I have inlued this

header('Content-Type: text/html; charset=UTF-8');

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_regex_encoding('UTF-8');

Hmmm.. now how to add this in to my phalcon app...



23.6k
Accepted
answer
edited Mar '16

Ok i have fixed it .. It was a wrong place where I put this charset option. Since Im usig Phalcon PDO .. i had to put it in there, not in an external config file..

        "options" => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
        )