Getting data with UTF-8 charset from MSSQL server using PHP FreeTDS extension(使用 PHP FreeTDS 扩展从 MSSQL 服务器获取带有 UTF-8 字符集的数据)
问题描述
我似乎无法使用 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 字符集的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHP FreeTDS 扩展从 MSSQL 服务器获取带有 UTF-8 字符集的数据
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01