|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-09-09 01:30 UTC] aireater at gmail dot com
Description: ------------ On PHP 5.1.6 Windows binary with php_mbstring and php_mssql.dll enabled, it fails to convert multi-byte UTF-8 strings to UCS-2 srtings. I've tried MS SQL Server 2005 Express and Standard but no success. On Windows 2003 Server. I've also tried several ntwdblib.dll but in vain. Expected result: ---------------- Should correctly convert multi-byte UTF-8 strings to UCS-2. Actual result: -------------- Most of single byte UTF-8 strings seem to be stored in the database but most of multi-byte UTF-8 strings get garbage. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 10:00:01 2025 UTC |
This is a sample script to reproduce the issue. Create a table with a "name" column of a varchar type only before testing. The string "DD" means the string is consisted of multi-byte UTF-8 cahracters. Some are inserted correctly, but most of time, inserted strings are not correctly shown on the screen because they are not correctly inserted in UCS-2. This script is for MS SQL Server 2005 Express. <?php $server = "localhost\sqlexpress"; $username = "username"; $password = "password"; $sqlconnect = mssql_connect($server, $username, $password); $sqldb = mssql_select_db("testdb",$sqlconnect); $str = 'DD'; $sqlquery = "insert into testtable (name) values('".$str."');"; $results = mssql_query($sqlquery); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <?php $result = mssql_query("SELECT * FROM testtable"); while ($row=mssql_fetch_array($result)){ echo $row['name']."<br>\n";} mssql_close($sqlconnect); ?>In the prior sample, the mbstring was used just to set the mb_internal_encoding to UTF-8 in php.ini. In another case as below in which I converted a string from UTF-8 to UCS2 before inserted but it did not work either. I'm not sure and I don't care which func should correctly convert a string to UCS2. I just want to insert a multi-byte unicode string to MS SQL Server. <?php $server = "GATEWAY\sqlexpress"; $username = "sugardb"; $password = "ryu3513"; $sqlconnect = mssql_connect($server, $username, $password); $sqldb = mssql_select_db("testdb",$sqlconnect); $str = 'そして千葉ですこれはどうです'; $cstr = mb_convert_encoding($str,"UCS2","UTF-8"); $sqlquery = "insert into testtable (name) values(N'".$cstr."');"; $results = mssql_query($sqlquery); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <?php $result = mssql_query("SELECT * FROM testtable"); while ($row=mssql_fetch_array($result)){ echo mb_convert_encoding($row['name'],"UTF-8","UCS2")."<br>\n";} mssql_close($sqlconnect); ?>Could you replace the prior sample with this one? The old one has some authentification info. <?php $server = "localhost\sqlexpress"; $username = "userid"; $password = "password"; $sqlconnect = mssql_connect($server, $username, $password); $sqldb = mssql_select_db("testdb",$sqlconnect); $str = 'DDDDDDDD'; $cstr = mb_convert_encoding($str,"UCS2","UTF-8"); $sqlquery = "insert into testtable (name) values(N'".$cstr."');"; $results = mssql_query($sqlquery); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <?php $result = mssql_query("SELECT * FROM testtable"); while ($row=mssql_fetch_array($result)){ echo mb_convert_encoding($row['name'],"UTF-8","UCS2")."<br>\n";} mssql_close($sqlconnect); ?>