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 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
