在Zend Framework中,您可以通过在模块配置文件(例如module.config.php
)中设置driver_options
来实现每次连接时运行SET NAMES utf8
。以下是如何实现这一目标的示例:
module.config.php
)。service_manager
下的factories
数组中找到Zend\Db\Adapter\Adapter
的工厂。driver_options
。以下是一个示例:
return [
'service_manager' => [
'factories' => [
Zend\Db\Adapter\Adapter::class => function ($container) {
$config = $container->get('config');
$dbAdapterConfig = $config['db'];
$adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
$adapter = $adapterFactory->createService($container, 'Zend\Db\Adapter\Adapter', $dbAdapterConfig);
// 在此处添加驱动程序选项以设置字符集
$driver = $adapter->getDriver();
$driver->getConnection()->connect();
$driver->getConnection()->execute('SET NAMES utf8');
return $adapter;
},
],
],
];
这将确保每次连接到数据库时,都会运行SET NAMES utf8
,从而将字符集设置为UTF-8。
领取专属 10元无门槛券
手把手带您无忧上云