note 80076 modified in ref.mssql by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 02:01:37 +0000 |
| Subject: | note 80076 modified in ref.mssql by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155618@lists.php.net to get a copy of this message | ||
To write Unicode (nvarchar) to SQL2000/2005 use PHP
ç¨PHPå¨SQL Server ååUnicodeè³æ
----------------------------------------------
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:
<?php
$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);
?>
--was--
To write Unicode (nvarchar) to SQL2000/2005 use PHP
ç¨PHPå¨SQL Server ååUnicodeè³æ
----------------------------------------------
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);
--------------------------------------
reference by etraxis@gmail.com
http://php.net/manual/en/ref.mssql.php