Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP, MSSQL 200, ntext/nvarchar

MS SQL php mssql

  • Please log in to reply
2 replies to this topic

#1 wilso417

wilso417

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 13 October 2008 - 02:46 PM

I've been searching online for over a day and have not found a solution.

I am using IIS 6.0, PHP, and a MSSQL 2000 database and i'm trying to make a multi-language website. I am using PHP ODBC to connect to the database. I know that ntext/nvarchar fields are UCS-2/UTF-16 Unicode data but I want it outputed on the web page as UTF-8. When the page displays the output has a lot of ? instead of the characters.

Anyone have any idea? I know utf8_encode() will solve this issue, but I cannot use this on my overall system. I've tried using the mb_string parameters in the php.ini to force utf -8 but this does not work
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 October 2008 - 05:19 PM

Any translation between Unicode encodings is likely to result in issues like this. In particular, I'm pretty sure UTF-8 is just too limited to display all the characters available in UTF-16. You'll have to either change the character set of your website or live with characters not displaying.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 djnos14

djnos14

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 13 April 2010 - 07:44 AM

Hi !

I put my comment too late I think but if my answer can ever help you and others...
I was in front of the same mater and i resolved it like this :

//retrieve a PDO handler instance
$dbHd=Tools_Data_PDOBridge::getInstance(Tools_Data_PDOBridge::PDO_MSSQL, 'server', 'db', 'user', 'pwd');
$srcStr='éè$^^Ɇ';
$dataToInsert=mb_convert_encoding($srcStr, 'UCS-2LE', 'UTF-8');
$arr = unpack('H*hex', $dataToInsert);

var_dump($arr);

$hex = "0x{$arr['hex']}";
$str='insert into CIVILITE(CIV_LIBELLE, test) values(\''.mb_convert_encoding('éè$^^É', 'ISO-8859-1', 'UTF-8').'\', '.$hex.');';
$dbHd->exec($str);

echo $srcStr.PHP_EOL;

$st=$dbHd->query('select top 1 convert(varbinary(200), test) as test from CIVILITE where CIV_ID>3;');

$civ=$st->fetch(PDO::FETCH_ASSOC);

var_dump($civ['test']);

$rtData=pack('H*', $civ['test']);
echo iconv('UCS-2LE', 'UTF-8', $rtData);


Bye! :)
  • 0





Also tagged with one or more of these keywords: MS SQL, php, mssql

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download