Php/ODBC encoding problem

时间秒杀一切 提交于 2019-11-29 16:47:31
Nicolas Comberu

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!