catbelly
catbelly

Reputation: 500

Inserting Unicode characters with PHP -> ODBC -> MS SQL?

I have the following code:

$sql = "update tbl_test set category = N'resumé';

echo $sql;

$rs=odbc_exec($conn,$sql);

Where $conn is a DSN ODBC connection to an MSSQL Server. The problem seems to be that somewhere between PHP and MySQL (Maybe ODBC?) unicode characters are converted to junk. If I copy paste exactly what the echo says directly into Enterprise Manager, it inserts into MS SQL fine. If I run the code however, it always goes into MSSSQL as resumé . Any idea what I'm missing here?

Upvotes: 5

Views: 3385

Answers (3)

Wez Furlong
Wez Furlong

Reputation: 4987

If you are on Windows and running PHP 5.3, using PDO_ODBC with SQL Native Client, and your input text is UTF-8, this should just work automatically.

Upvotes: 1

TheJacobTaylor
TheJacobTaylor

Reputation: 4143

I would tread carefully here. In the past, when dealing with SQL Server and PHP integration, I have encountered cases where SQL Server uses between 3 and 5 times as much storage space as is actually needed for Unicode characters. Depending on how you connect to the database and how the data is encoded. My understanding is that MSFT is working on a new driver (or it has possibly been out for a while) that addresses the issue and generally improves PHP integration. With some of the older stacks, you might run into trouble. I recommend looking at the data size in the DB to verify that the connection is not padding your data.

Jacob

Upvotes: 0

catbelly
catbelly

Reputation: 500

The problem was not ODBC, but PHP... solution was to use utf8_decode() on the strings returned from the database.

Upvotes: 4

Related Questions