ID: 38760 User updated by: aireater at gmail dot com Reported By: aireater at gmail dot com Status: Open Bug Type: MSSQL related Operating System: Windows 2003 Server PHP Version: 5.1.6 New Comment:
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); ?> Previous Comments: ------------------------------------------------------------------------ [2006-09-09 13:31:16] aireater at gmail dot com 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); ?> ------------------------------------------------------------------------ [2006-09-09 13:11:22] [EMAIL PROTECTED] What makes you think that this code should convert something to UCS-2? What does it have to do with mbstring, which is even not mentioned in this code? ------------------------------------------------------------------------ [2006-09-09 13:04:57] aireater at gmail dot com 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); ?> ------------------------------------------------------------------------ [2006-09-09 12:12:25] [EMAIL PROTECTED] Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2006-09-09 12:04:30] aireater at gmail dot com Set mb_internal_encoding to UTF-8, and send an INSERT statement, that inserts a multi-byte string in UTF-8 to a varchar culumn, then an inserted string get garbage. It's not converted to UCS-2 that MS SQL Server supports to store an unicode string. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/38760 -- Edit this bug report at http://bugs.php.net/?id=38760&edit=1