开发者

UTF-8 in SQL Server 2008 Database + PHP

开发者 https://www.devze.com 2023-04-01 04:17 出处:网络
I want to store data with PHP in a MS SQL 2008 Database. I`ve got problems with letter开发者_运维百科s like ä ö ü ß, they are displayed incorrect in the database and when i display it on the webs

I want to store data with PHP in a MS SQL 2008 Database.

I`ve got problems with letter开发者_运维百科s like ä ö ü ß, they are displayed incorrect in the database and when i display it on the website.

It works when I utf8_encode the data on input and utf8_decode on output the data with PHP.

Is there a other easier way to solve this?


I've solved this once, the problem is that the PHP's mssql driver is broken (can't find the link to bugs.php.net, but it is there) and fails when it comes to nchar and nvarchar fieldsa and utf8. You'll need to convert the data and queries a little bit:

SELECT some_nvarchar_field FROM some_table

First, you need to change the output to binary - that way it won't get corrupted:

SELECT CONVERT(varbinary(MAX), some_nvarchar_field) FROM some_table;

Then in PHP, you'll need to convert it to UTF-8 back, there you'll need iconv extension

iconv('UTF-16LE', 'UTF-8', $result['some_nvarchar_field']);

This fixes selectiong data from database, however, if you want to actually put some data TO the database, or add a WHERE clause, you'll still be getting errors, so the fix for WHERE, UPDATE, INSERT and so on is by converting the string to hexadecimal form:
Imagine you have this query:

INSERT INTO some_table (some_nvarchar_field) VALUES ('ŽČŘĚÝÁÖ');

Now, we'll have to run some PHP:

$value = 'ŽČŘĚÝÁÖ';
$value = iconv('UTF-8', 'UTF-16LE', $value); //convert into native encoding 
$value = bin2hex($value); //convert into hexadecimal
$query = 'INSERT INTO some_table (some_nvarchar_field)  VALUES(CONVERT(nvarchar(MAX), 0x'.$value.'))'; 

The query becomes this:

INSERT INTO some_table (some_nvarchar_field) VALUES (CONVERT(nvarchar(MAX), 0x7d010c0158011a01dd00c100d600));

And that will work!

I've tested this with MS SQL server 2008 on Linux using FreeTDS and it works just fine, I've got some huge websites runing on this with no issues what so ever.


I searched for two days how to insert UTF-8 data (from web forms) into MSSQL 2008 through PHP. I read everywhere that you can't, you need to convert to UCS2 first (like cypher's solution recommends). On Windows environment SQLSRV said to be a good solution, which I couldn't try, since I am developing on Mac OSX.

However, FreeTDS manual (what PHP mssql uses on OSX) says to add a letter "N" before the opening quote:

mssql_query("INSERT INTO table (nvarcharField) VALUES (N'űáúőűá球最大的采购批发平台')", +xon);

According to this discussion, N character tells the server to convert to Unicode. https://softwareengineering.stackexchange.com/questions/155859/why-do-we-need-to-put-n-before-strings-in-microsoft-sql-server


i can insert by code

    $value = $_POST['first_name'];
    $value = iconv("UTF-8","UCS-2LE",$value);
    $value2 = $_POST['last_name'];
    $value2 = iconv("UTF-8","UCS-2LE",$value2);  
    $query = "INSERT INTO tbl_sample(first_name, last_name) VALUES (CONVERT(VARBINARY(MAX), '".$value."') , CONVERT(VARBINARY(MAX), '".$value2."'))"; 

    odbc_exec($connect, $query);

if i can not search

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号