問題描述
我似乎無法使用 FreeTDS 擴展從 MSSQL 中獲取編碼為 UTF-8 的數據.
I can't seem to get data from MSSQL encoded as UTF-8 using FreeTDS extension.
連接:
ini_set('mssql.charset', 'UTF-8');
$this->_resource = mssql_connect($config['servername'], $config['username'], $config['password']);
我無法使用任何其他擴展程序.
I have no ability to use any other extension.
我試過創建 ~/.freetds.conf
I've tried creating ~/.freetds.conf
[global]
client charset = UTF-8
我試過向 php 傳遞參數:
I've tried passing parameters to php:
php -d mssql.charset="UTF-8" index.php
數據仍然不是 UTF-8.
Data is still not in UTF-8.
php -i
mssql
MSSQL Support => enabled
Active Persistent Links => 0
Active Links => 0
Library version => FreeTDS
Directive => Local Value => Master Value
mssql.allow_persistent => On => On
mssql.batchsize => 0 => 0
mssql.charset => no value => no value
mssql.compatability_mode => Off => Off
mssql.connect_timeout => 5 => 5
mssql.datetimeconvert => On => On
mssql.max_links => Unlimited => Unlimited
mssql.max_persistent => Unlimited => Unlimited
想法?
推薦答案
MSSQL 和 UTF-8 在……有時是相當痛苦的.我不得不手動轉換它.問題:MSSQL 實際上并不知道和支持 UTF-8.
MSSQL and UTF-8 are quite a pain in the ... sometimes. I had to convert it manually. The problem: MSSQL doesn't actually know and support UTF-8.
從數據庫值轉換為 UTF-8:
Convert from database value to UTF-8:
mb_detect_encoding($value, mb_detect_order(), true) === 'UTF-8' ? $value : mb_convert_encoding($value, 'UTF-8');
從 UTF-8 轉換為數據庫值:
Converting from UTF-8 to database value:
mb_convert_encoding($value, 'UCS-2LE', mb_detect_encoding($value, mb_detect_order(), true));
幸運的是我使用了 Doctrine,所以我所要做的就是創建一個自定義的 StringType 實現.
Fortunately I was using Doctrine so all I had was to create a custom StringType implementation.
這篇關于使用 PHP FreeTDS 擴展從 MSSQL 服務器獲取帶有 UTF-8 字符集的數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!