FreeTDS - 返回UTF-8字符集数据

时间:2014-08-14 18:19:49

标签: php pdo iconv freetds

我正在使用带有PHP PDO的MS SQL Server 2008:我有一个PHP PDO类,它采用Windows字符集并手动将数据转换为UTF-8。类似的东西:

$sql = "SELECT * FROM something_somewhere);
$query = $this->db->query($sql);
$result = $query->fetchAll(PDO::FETCH_OBJ);
foreach ($result as $row) {
foreach (get_object_vars($row) as $key => $value) {
    $row->$key = (mb_detect_encoding($value, mb_detect_order(), true) === 'UTF-8') 
            ? $value : iconv('iso-8859-1', 'utf-8', $value);
}
$results[] = $row;

}

iconv()效果很好,但是一直写这个很乏味。有没有配置FreeTDS转换为UTF-8?

我正在阅读Moodle的FreeTDS配置:

https://docs.moodle.org/22/en/FreeTDS

我当前的/etc/freetds.conf文件(不可编辑,所以我必须让管理员更改它):

#   $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 *** Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".

# Global settings are overridden by those in a database
# server specific section
[global]
        # TDS protocol version
;       tds version = 4.2

        # Whether to write a TDSDUMP file for diagnostic purposes
        # (setting this to /tmp is insecure on a multi-user system)
;       dump file = /tmp/freetds.log
;       debug flags = 0xffff

        # Command and connection timeouts
;       timeout = 10
;       connect timeout = 10

        # If you get out-of-memory errors, it may mean that your client
        # is trying to allocate a huge buffer for a TEXT field.
        # Try setting 'text size' to a more reasonable limit
        text size = 64512

# A typical Sybase server
[egServer50]
        host = symachine.domain.com
        port = 5000
        tds version = 5.0

# A typical Microsoft server
[egServer70]
        host = ntmachine.domain.com
        port = 1433
        tds version = 7.0

我假设我需要编辑[全局]设置,同时添加:

tds version = 7.0

客户端字符集= UTF-8

任何人都可以确认吗?

1 个答案:

答案 0 :(得分:5)

是的,您需要在freetds配置文件中设置UTF。

  1. 打开freetds.conf
  2. 在您的连接设置中添加client charset = UTF-8