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 字符集的数据
基础教程推荐
- 使用 PDO 转义列名 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01