沃梦达 / 编程问答 / php问题 / 正文

使用 PHP FreeTDS 扩展从 MSSQL 服务器获取带有 UTF-8 字符集的数据

Getting data with UTF-8 charset from MSSQL server using PHP FreeTDS extension(使用 PHP FreeTDS 扩展从 MSSQL 服务器获取带有 UTF-8 字符集的数据)

本文介绍了使用 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 字符集的数据

基础教程推荐