MYSQL_ATTR_INIT_COMMAND error

Deadly 提交于 2019-12-25 06:05:11

问题


I'm new to Zend Framework and I'm learning how to create an application following the steps in the framework.zend webpages.

I have a problem with the global.php page.

my global.php is:

return array(
     'db' => array(
         'driver'         => 'Pdo',
         'dsn'            => 'mysql:dbname=zf2tutorial;host=127.0.0.1;charset=utf8',
         'driver_options' => array(
             PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
         ),
     ),
     'service_manager' => array(
         'factories' => array(
             'Zend\Db\Adapter\Adapter'
                     => 'Zend\Db\Adapter\AdapterServiceFactory',
         ),
     ),
 );

I'd like to know what the instruction PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'" is for, because it gives me an error if I run the application on localhost:8080.

This is the error: Fatal error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' in C:\DevBox\zfproject\config\autoload\global.php

Can I remove that line? And If I cannot remove it how can I fix the problem?

I'm on windows and everything works if I remove that line but I don't think it's the right solution.


回答1:


Yes, you can remove that line.

Actually, it's just a some sort of gospel, or mantra, each PHP user recites without actually knowing the meaning. It's intended to set default charset, which is already set in DSN (although effective since PHP 5.3.6)

Nevertheless, you can also perpend a class name with backslash to solve the problem, to make PDO class accessible from within any namespace.



来源:https://stackoverflow.com/questions/31655165/mysql-attr-init-command-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!