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

Mysql UFT8 not working on Phalcon Micro

Hi, I've already made a lot of research and tried many configurations but still get the same error so here is the case;

I have a phalcon MVC app running on a database and everything works fine it can retrive all data from in UTF-8 with no problem but I'm writing a micro app that retrives data from the same database and it can't ready special caracteres.

I tried: on my services.php and config,php files

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

and also 'charset'=> 'utf8'

Tried this as well:

/**

  • Database connection is created based in the parameters defined in the configuration file */ $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' => $config->database->charset )); });

but seams phalcon is not reading de services.php file.

Any help would be appreciated.

Thanks in advance.



85.5k
edited Oct '15

dont use the config, just hardcode it inside

$this->di->set('db', function() use ($config) {

            $class = new \Phalcon\Db\Adapter\Pdo\Mysql([
                'host' => $config->database->host,
                'username' => $config->database->username,
                'password' => $config->database->password,
                'dbname' => $config->database->dbname,
                'port' => '3306',
                'charset' => 'utf8',
                "options"  => [
                    \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                    \PDO::ATTR_PERSISTENT => true,
                    \PDO::ATTR_EMULATE_PREPARES => false,
                    \PDO::ATTR_DEFAULT_FETCH_MODE  =>  \PDO::FETCH_ASSOC,
                    \PDO::ATTR_STRINGIFY_FETCHES => false
                ]
            ]);

            return $class;
        }, true);

Hi @Izo thanks for prompt response, I've just tried it and didn't work for me I don't know why but seams like it is not reading my services file because if I remove all paremeters from connection from the file it still connecting to the database.

dont use the config, just hardcode it inside

$this->di->set('db', function() use ($config) {

           $class = new \Phalcon\Db\Adapter\Pdo\Mysql([
               'host' => $config->database->host,
               'username' => $config->database->username,
               'password' => $config->database->password,
               'dbname' => $config->database->dbname,
               'port' => '3306',
               'charset' => 'utf8',
               "options"  => [
                   \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                   \PDO::ATTR_PERSISTENT => true,
                   \PDO::ATTR_EMULATE_PREPARES => false,
                   \PDO::ATTR_DEFAULT_FETCH_MODE  =>  \PDO::FETCH_ASSOC,
                   \PDO::ATTR_STRINGIFY_FETCHES => false
               ]
           ]);

           return $class;
       }, true);


85.5k

multi module app or single module app ?

single module I guess $app = new Application\Micro();

multi module app or single module app ?



85.5k

ah yeah, sorry i am stupid, so can oyu post your connection in a proper display way

https://forum.phalcon.io/help/create-post

edited Oct '15

Solved the problem the settings was in library/application/Micro.php not in the services.php I just added the line

'charset' => $di->get('config')->database->charset

and everything worked.. Thanks a lot @Izo Couldn't do without your help.

Cheeeerss!!!! :)