|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-07-04 18:10 UTC] joesterg at hotmail dot com
I have a problem converting UTF-8 (web character encoding) to UCS2 (Microsoft Windows character encoding) using PHP, and storing this in the Microsoft SQL Server 2000 database. My setup is: Windows 2000 Server, with Apache 1.3.24/PHP 4.1.1 and Microsoft SQL Server 2000 Now, as a result of Microsofts Q232580, I will have to do conversion between UTF-8 and UCS-2. For this, I thought I would use the Multibyte String functions. However, this does not seem to work. I am absolutely sure, that I input UTF-8 encoded data into my string, and then I do: $ucs2string=mb_convert_encoding($string,"UCS2","UTF-8"); $sqlStmt="insert into testtbl (tekst) values(N'".($ucs2string)."')"; $rs=$DBCon->Execute($sqlStmt); When I access the database, then I will see something stored, that does not resemble the input at all (most times, I see Japanese/Chinese characters?!??). Furthermore, the insert sometimes comes up with an error, and consequently stores nothing. To me, it seems like either one of these (or both) are flawed: 1. the Multibyte String encoding funtion does not work properly (ie. encoding from UTF-8 to UCS-2 does not happen correctly). 2. The PHP MSSQL driver does not handle unicode data properly, even though the target column in the database is specified as Unicode and N is prepended to the string before insert. This leads me to use ADO (as in the example above), storing UTF-8 encoded data into SQL Server -this is a very short term solution, as data are not sortable in the database (some of it looks like garbage because of the missing encoding). PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 21:00:01 2025 UTC |
If you're using PHP on a Windows platform you can use the PHP COM extension to communicate with SQL Server via ADO. The PHP COM extension is capable of translating UTF-8 to UCS-2 and back if you specify so as the third parameter: $oDb = new COM('ADODB.Connection', NULL, CP_UTF8); This way you can use Unicode UTF-8 within PHP and Unicode UCS-2 within SQL Server with all the translations done for you automatically. HTH, Freddy VultoI want to connect php with MS SQL Server 2000 I have used the folloing code in PHP: <? $connection = mysql_connect ("localhost","user name","password"); if (!$connection) { echo "Couldn't make a connection!"; exit; } ?> The code doesn't execute.Looks like nobody is going to fix the issue. ;( I haven't solution but I have workaround that I use in my project and it works - it's sending and receiving data as binary. ========= Example ========= Let's assume, we have following data table, that allows us to store unicode values (using UCS-2 encoding): create table mytable ( myfield nvarchar (100) null ); Below is the code to work with: $link = mssql_connect('dbhost', 'username', 'password'); mssql_select_db('database', $link); // sending data to database $utf8 = 'some unicode UTF-8 data'; // some Greek text for example ;) $ucs2 = iconv('UTF-8', 'UCS-2LE', $utf8); // converting UCS-2 string into "binary" hexadecimal form $arr = unpack('H*hex', $ucs2); $hex = "0x{$arr['hex']}"; // IMPORTANT! // please note that value must be passed without apostrophes // it should be "... values(0x0123456789ABCEF) ...", not "... values('0x0123456789ABCEF') ..." mssql_query("insert into mytable (myfield) values ({$hex})", $link); // retrieving data from database // IMPORTANT! // please note that "varbinary" expects number of bytes // in this example it must be 200 (bytes), while size of field is 100 (UCS-2 chars) $result = mssql_query("select convert(varbinary(200), myfield) from mytable", $link); while (($row = mssql_fetch_array($result, MSSQL_BOTH))) { // we get data in UCS-2 // I use UTF-8 in my project, so I encode it back echo(iconv('UCS-2LE', 'UTF-8', $row['myfield'])); } mssql_free_result($result); mssql_close($link);