LoginSignup
7
8

More than 5 years have passed since last update.

Phalcon + MySQL の文字化けに対処する

Posted at

PHP 5.3.6 以降の場合は、PDO に charset パラメータがあるのでそれで指定する。

services.php などで、

use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
:
$di->set('db', function() use ($config) {
  $dbAdapter = new DbAdapter([
    'host' => 'HOST',
    'username' => 'USERNAME',
    'password' => 'PASSWORD',
    'dbname' => 'DBNAME',
    'charset' => 'utf8'
  ]);
  :

PHP 5.3.6 より前の場合は、お手軽に SET NAMES utf8 で一応対処できる。(このやり方はよろしくないことが起こることがあるらしいがちゃんと調べてない)

use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
:
$di->set('db', function() use ($config) {
  $dbAdapter = new DbAdapter(array(
    'host' => 'HOST',
    'username' => 'USERNAME',
    'password' => 'PASSWORD',
    'dbname' => 'DBNAME',
    'options' => array(
      \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
    )
  ));
  :
7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8