note 80076 added to ref.mssql

From: Date: Fri, 28 Dec 2007 01:20:30 +0000
Subject: note 80076 added to ref.mssql
Groups: php.notes 
Request: Send a blank email to php-notes+get-134059@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: $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 ---- Server IP: 140.112.2.5 Probable Submitter: 219.87.67.30 ---- Manual Page -- http://www.php.net/manual/en/ref.mssql.php Edit -- https://master.php.net/note/edit/80076 Del: integrated -- https://master.php.net/note/delete/80076/integrated Del: useless -- https://master.php.net/note/delete/80076/useless Del: bad code -- https://master.php.net/note/delete/80076/bad+code Del: spam -- https://master.php.net/note/delete/80076/spam Del: non-english -- https://master.php.net/note/delete/80076/non-english Del: in docs -- https://master.php.net/note/delete/80076/in+docs Del: other reasons-- https://master.php.net/note/delete/80076 Reject -- https://master.php.net/note/reject/80076 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#134059) next »