php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38760 php_mssql fails to convert multi-byte UTF-8 to UCS-2
Submitted: 2006-09-09 01:30 UTC Modified: 2006-09-09 13:42 UTC
From: aireater at gmail dot com Assigned:
Status: Not a bug Package: MSSQL related
PHP Version: 5.1.6 OS: Windows 2003 Server
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: aireater at gmail dot com
New email:
PHP Version: OS:

 

 [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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-09 10:39 UTC] tony2001@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.



 [2006-09-09 12:04 UTC] 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.
 [2006-09-09 12:12 UTC] tony2001@php.net
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 13:04 UTC] 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 13:11 UTC] tony2001@php.net
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:31 UTC] 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 = '&#12381;&#12375;&#12390;&#21315;&#33865;&#12391;&#12377;&#12371;&#12428;&#12399;&#12393;&#12358;&#12391;&#12377;';
$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:38 UTC] aireater at gmail dot com
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);
?>
 [2006-09-09 13:42 UTC] tony2001@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC