Zend Frameworkï¼MySQLã®æååã対ç
PHP5 ï¼ Zend Framework 1.9.2 ï¼ MySQL5 ã§ãDBãããã¼ã¿ãåå¾ããã¨ãã«æååãããã®ãé²ãæ¹æ³ã«ã¤ãã¦ã
以ä¸ã®ããã«ãset names utf8ããå ã«çºè¡ãã¦ããã°æååãã¯é²ããããæ±ç¨ç㪠Zend_Db ã使ã£ã¦ãã以ä¸ããã¯ã MySQL ç¹æã®ã³ãã³ãã¯ä½¿ããããªããSQLã¤ã³ã¸ã§ã¯ã·ã§ã³ãå¼ãèµ·ããå¯è½æ§ãããã¾ããï¼åèãSET NAMESã¯ç¦æ¢ï¼http://blog.ohgaki.net/set_namesa_mcb_ascï¼
$db = Zend_Db::factory('Pdo_Mysql', array( 'host' => 'xxx.xxx.xxx.xxx', 'username' => 'xxx', 'password' => 'xxx', 'dbname' => 'xxx' )); $db->query("set names utf8");
ããã§èª¿ã¹ã¦ã¿ãã¨ãfactory() ã®ãã©ã¡ã¼ã¿ã« charset ãæå®ã§ãããã¨ãå¤æã以ä¸ã®ããã«ããã¨ãæååããé²ããã¨ãã§ãã¾ããã
$db = Zend_Db::factory('Pdo_Mysql', array( 'host' => 'xxx.xxx.xxx.xxx', 'username' => 'xxx', 'password' => 'xxx', 'dbname' => 'xxx', 'charset' => 'utf8' ));
ããã§ããããã»ã»ã»ã¨æã£ãã®ã§ããã念ã®ãã Zend Framework ã®ã½ã¼ã¹ã³ã¼ãï¼Zend/Db/Adapter/Pdo/Mysql.phpï¼ãè¦ã¦ã¿ãã¨ããset names xxxãã使ã£ã¦ãããããªãã§ããï¼ãPDO ã® driver_options ãã©ã¡ã¼ã¿ã«ãset names xxxãã渡ãã¦ãã¾ãããSQLã¤ã³ã¸ã§ã¯ã·ã§ã³ã¯å¤§ä¸å¤«ãªã®ã§ããããã»ã»ã»ï¼
以ä¸ãã½ã¼ã¹ã³ã¼ãã®ä¸é¨å¼ç¨ã§ãã
protected function _connect() { if ($this->_connection) { return; } if (!empty($this->_config['charset'])) { $initCommand = "SET NAMES '" . $this->_config['charset'] . "'"; $this->_config['driver_options'][1002] = $initCommand; // 1002 = PDO::MYSQL_ATTR_INIT_COMMAND } parent::_connect(); }
ã¡ãã£ã¨ä¸å®ãªã®ã§ãPdo_Mysql ã§ã¯ãªã Mysqli ã使ããã¨ã«ãããã§æååãããã«ãã¾ãåãã¾ããã
$db = Zend_Db::factory('Mysqli', array( 'host' => 'xxx.xxx.xxx.xxx', 'username' => 'xxx', 'password' => 'xxx', 'dbname' => 'xxx', 'charset' => 'utf8' ));
Mysqli ã® ã½ã¼ã¹ã³ã¼ãï¼Zend/Db/Adapter/Mysqli.phpï¼ãè¦ãã¨ãmysqli_set_charset() ã§æåã³ã¼ããã»ãããã¦ãã¾ãããããã使ãã°ãSQLã¤ã³ã¸ã§ã¯ã·ã§ã³ã¯å¤§ä¸å¤«ãªããã§ãã