php/ODBC编码问题

发布于 2024-08-30 20:06:22 字数 744 浏览 3 评论 0原文

我使用 ODBC 从 PHP 连接到 SQL Server。 在 PHP 中,我从 SQL Server 读取一些字符串(nvarchar 列)数据,然后想将其插入到 mysql 数据库中。当我尝试将这样的值插入 mysql 数据库表时,我收到此 mysql 错误:

Incorrect string value: '\xB3\xB9ow...' for column 'name' at row 1

对于包含所有 ASCII 字符的字符串,一切都很好,当存在非 ASCII 字符(来自某些欧洲语言)时,就会出现问题。

因此,更一般地说:MS SQL Server 数据库中有一个 Unicode 字符串,PHP 通过 ODBC 检索该字符串。然后将其放入针对 mysql 数据库执行的 sql 插入查询(作为 utf-8 varchar 列的值)中。

有人可以向我解释一下在这种情况下编码方面发生了什么吗?在哪一步可能会发生什么字符编码转换?

我使用:PHP 5.2.5、MySQL5.0.45-community-nt、MS Sql Server 2005。PHP

必须在 Linux 平台上运行。

更新:当我在此字符串上调用 utf8_encode($s) 并在 mysql 插入查询中使用该值时,不会发生错误,但插入的字符串在 mysql 数据库中无法正确显示(因此utf8 编码仅适用于强制执行正确的 utf8 字符串,但它会丢失正确的字符)。

I use ODBC to connect to SQL Server from PHP.
In PHP I read some string (nvarchar column) data from SQL Server and then want to insert it to mysql database. When I try to insert such value to mysql database table I get this mysql error:

Incorrect string value: '\xB3\xB9ow...' for column 'name' at row 1

For string with all ASCII characters everything is fine, the problem occurs when non-ASCII characters (from some European languages) exist.

So, in more general terms: there is a Unicode string in MS SQL Server database, which is retrieved by PHP trough ODBC. Then it is put in sql insert query (as value for utf-8 varchar column) which is executed for mysql database.

Can someone explain to me what is happening in this situation in terms of encoding? At which step what character encoding convertions may take place?

I use: PHP 5.2.5, MySQL5.0.45-community-nt, MS Sql Server 2005.

PHP have to run on Linux platform.

UPDATE: The error doesn't occur when I call utf8_encode($s) on this string and use that value in mysql insert query, but then the inserted string doesn't display correctly in mysql database (so that utf8 encoding only worked for enforcing proper utf8 string, but it loses correct characters).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的往事 2024-09-06 20:06:22

首先你有数据库的编码。然后您就可以得到 ODBC 客户端使用的编码。

如果 ODBC 客户端连接的编码与数据库的编码不匹配,在某些情况下,ODBC 层将自动对您的数据进行转码。

这里的技巧是强制 ODBC 客户端连接的编码。

对于“全 UTF-8”设置:

$conn=odbc_connect(DB_DSN,DB_USR,DB_PWD);
odbc_exec($conn, "SET NAMES 'UTF8'");
odbc_exec($conn, "SET client_encoding='UTF-8'");

// processing here

这与 PostgreSQL + Php 5.x 完美配合。
确切语法和选项取决于数据库供应商。

您可以在这里找到有关 MySql 的非常有用且清晰的附加信息:http: //dev.mysql.com/doc/refman/5.0/fr/charset-connection.html

希望这有帮助。

First you have the encoding of the DB. Then you have the encoding used by the ODBC client.

If the encoding of your ODBC client connection does not match the one of the DB, the ODBC layer will automatically transcode your data, in some cases.

The trick here is to force the encoding of the ODBC client connection.

For an "all UTF-8" setup :

$conn=odbc_connect(DB_DSN,DB_USR,DB_PWD);
odbc_exec($conn, "SET NAMES 'UTF8'");
odbc_exec($conn, "SET client_encoding='UTF-8'");

// processing here

This works perfectly with PostgreSQL + Php 5.x.
The exact syntax and options depends on the DB vendor.

You can find very useful and clear additional info for MySql here : http://dev.mysql.com/doc/refman/5.0/fr/charset-connection.html

hope this helps.

放赐 2024-09-06 20:06:22

也许您可以使用 PDO 扩展 ,这会有什么不同吗?

此处有一条用户贡献的评论 建议将 sql server 中的数据类型更改为其他类型,如果这不可能,请查看转换字段的用户类。

Maybe you can use the PDO extension, if it will make any difference?

There is a user contributed comment here that suggests to change the data types in sql server to somethig else, if this is not possible look at the users class that casts fields.

圈圈圆圆圈圈 2024-09-06 20:06:22

我没有通过 PHP 使用 ODBC 的经验,但是对于 mysql 函数,PHP 似乎默认使用 ASCII 和 UTF8 连接,如果您想避免麻烦,则需要显式连接。

您确定 PHP 和 MySQL 服务器以 UTF8 进行通信吗?在 PHP 6 之前,Unicode 支持往往会出现令人烦恼的不一致。

我记得 MySQL 文档提到了一个连接字符串参数来调整 Unicode 编码。

从您的描述来看,PHP 似乎将连接视为仅限 ASCII。

I have no experience with ODBC via PHP, but with the mysql functions PHP seems to default to ASCII and UTF8 connections need to be made explicit if you want to avoid trouble.

Are you sure PHP and the MySQL server are communicating in UTF8? Until PHP 6 the Unicode support tends to be annoyingly inconistent like that.

I remember that the MySQL docs mention a connection string parameter to tweak the Unicode encoding.

From your description it sounds like PHP is treating the connection as ASCII-only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文