日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/4/5 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);

}

總結

以上是生活随笔為你收集整理的yii2 pdo mysql 乱码_YII2.0使用PDO连接Oracle库查询结果中文显示乱码问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。