yii2 pdo mysql 乱码_YII2.0使用PDO连接Oracle库查询结果中文显示乱码问题

自己来解答,直接上代码。

创建链接测试用小程序如下:

namespace app\models;

use Yii;

use yii\db\ActiveRecord;

use \yii\db\Connection;

class CmsSyncData extends ActiveRecord

{

/**

* @inheritdoc

*/

public function attributeLabels()

{

return [

'id' => '新闻ID',

];

}

/**

* @inheritdoc

*/

public static function getDb()

{

return new Connection([

'dsn' => 'oci:dbname=//localhost:portno/dbname;charset=utf8',

'username' => 'XXX',

'password' => 'XXX',

]);

}

/**

* Finds news by $id

*

* @param string $id

* @return static|null

*/

public static function findByNewsId($id)

{

$connection = self::getDb();

$command = $connection->createCommand('SELECT * FROM content_news WHERE id='.$id);

$result = $command->queryOne();

return $result;

}

}

需要改YII2.0的代码:

\vendor\yiisoft\yii2\db\Connection.php

找到下面的函数,加上配置“, 'oci'”就可以了。

/**

* Initializes the DB connection.

* This method is invoked right after the DB connection is established.

* The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`

* if [[emulatePrepare]] is true, and sets the database [[charset]] if it is not empty.

* It then triggers an [[EVENT_AFTER_OPEN]] event.

*/

protected function initConnection(){

$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if ($this->emulatePrepare !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {

$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);

}

if ($this->charset !== null && in_array($this->getDriverName(), ['pgsql', 'mysql', 'mysqli', 'cubrid', 'oci'])) {

$this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset));

}

$this->trigger(self::EVENT_AFTER_OPEN);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值