PDO_ODBC的字符编码问题 [英] Character encoding issue with PDO_ODBC

查看:201
本文介绍了PDO_ODBC的字符编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PDO_ODBC从PHP访问Microsoft SQL数据库时使用以下代码,我有一个编码问题。当输出文本从DB被乱码。

When accessing a Microsoft SQL Database from PHP using PDO_ODBC with the following code, I have an encoding issue. When outputed the text from the DB is garbled.

$dsn = "odbc:DRIVER={SQL Server};SERVER=$hostname;DATABASE=$database;charset=UTF-8";
$pdo = new PDO($dsn,$username,$password);
$sql = "SELECT text FROM atable";
$result = $PDO->query($sql);
while($data = $result->fetchObject()){
  $values[] = $data->text; 
}
dpm($values);

乱码输出http://image.bayimg.com/naomcaacd.jpg

这是从Drupal模块完成的。 Drupal中的所有内容都与UTF-8配合使用。最干净的解决方案是能够在UTF-8中从数据库中检索数据,或者在输出之前转换为UTF-8。

This is done from a Drupal module. Everything in Drupal is made to work with UTF-8. The cleanest solution would be to able to retrieve the data from the database in UTF-8 or to convert it UTF-8 before outputting.

我没有成功尝试过这些


  • $ dsn =odbc:DRIVER = {SQL Server}; SERVER = $ hostname; DATABASE = $ database; client_charset = utf-8

  • $ dsn =odbc:DRIVER = {SQL Server}; SERVER = $ hostname; DATABASE = $ database; charset = utf-8

  • code> $ pdo-> exec('SET NAMES utf8') after new PDO(...)

  • $ pdo-> exec('SET CHARACTER SET utf8'); after new PDO(...) / code>

  • $dsn = "odbc:DRIVER={SQL Server};SERVER=$hostname;DATABASE=$database;client_charset=utf-8"
  • $dsn = "odbc:DRIVER={SQL Server};SERVER=$hostname;DATABASE=$database;charset=utf-8"
  • $pdo->exec('SET NAMES utf8') after new PDO(...)
  • $pdo->exec('SET CHARACTER SET utf8'); after new PDO(...)

PS:该代码目前在Windows上开发,但也必须在GNU / Linux上工作。 / p>

PS: The code is currently developped on Windows but it has to work on GNU/Linux too.

推荐答案

在Linux上运行并使用FreeTDS驱动程序时,客户端的字符集可以配置为客户端字符集设置在 freetds.conf 文件。为了在使用PDO ODBC和unixODBC时使用 freetds.conf 文件,需要使用 ODBC组合配置。当使用仅ODBC配置来配置ODBC数据源时,文件 freetds.conf 未使用。有了这个,我可以从MS SQL Server数据库中检索和插入UTF-8数据。

When running on Linux and using the FreeTDS driver, the charset for the client can be configured with the client charset setting in the freetds.conf file. In order for the freetds.conf file to be used when using PDO ODBC and unixODBC, one needs to configure the ODBC datasource using ODBC-combined configuration. When ODBC-only configuration is used to configure the ODBC datasource, the file freetds.conf is not used. With this I was able to retrieve and insert UTF-8 data from/into the MS SQL Server database.

作为一名Linux / Unix的人,我无法理解/找到一种方法来配置在Windows上使用PDO ODBC时使用的字符集。我的模糊理解是,当在系统级配置时,可以将ODBC数据源配置为使用de SQL Server数据库的字符集或转换为客户端计算机字符集。

Being a Linux/Unix guy, I was unable to understand/find a way how to configure the charset used when PDO ODBC is used on Windows. My vague understanding is that when configured at the system level, an ODBC datasource can be configured to use either de SQL Server database's charset or convert to the client computer charset.

这篇关于PDO_ODBC的字符编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆