gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:03:31 27 4
gpt4 key购买 nike

我正在使用带有 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

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

tds 版本 = 7.0

客户端字符集 = UTF-8

谁能证实一下?

最佳答案

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

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

关于php - FreeTDS - 返回 UTF-8 字符集数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25314965/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com