PHP sqlsrv 2 and UTF-8

please, what should I do to store data with UTF-8 into SQL Server (2005) right? My html page is UTF-8 coded, column in table is type nvarchar, connection charset is set to UTF-8, but russian string 'Логистика Другое' is stored as '????????? ??????' into table. I am using sqlsrv v2. Here is connection params:

$dbConnParams = array('server' => "server", // SQL Server
                      'params' => array("Database"      => "dbname",
                                        "UID"           => 'user',
                                        "PWD"           => 'pass',
                                        "CharacterSet"  => 'UTF-8'));

$dbh = sqlsrv_connect($dbConnParams['server'], $dbConnParams['params']);

(php 5.2.17, iis7.5, ms sql server 2005, sqlsrv 2)

Answers


It was caused by historical reason, when we used odbc without binding params:

$sql = "usp_cis_upd
            @Key = " . $key . ",
            @Name = '" . $name . "'";
odbc_exec($dbh, $sql);

if params are binded, strings are recoded and saved well:

$sql = "usp_cis_upd
            @Key = ?,
            @Name = ?";

$params = array($Key,
                $Name);

$stmt = sqlsrv_query($dbh, $sql, $params);

Most likely it is an issue with the set up of your database. The columns where the data is stored need to have the Unicode collation, or else it will not accept Unicode characters, resulting in question marks.

Using UTF-16 in your database may solve your problem. For a more in depth reason why, please see: http://blogs.msdn.com/b/qingsongyao/archive/2009/04/10/sql-server-and-utf-8-encoding-1-true-or-false.aspx


Need Your Help

Background service in android that generates notifications on database change

android android-service android-background

I am trying to write a service that runs in background and repeatedly calls to a web-service written in PHP. This web-service returns me JSON. I am getting the JSON but unable to fire a notificatio...

How to prevent css loaded with query being applied to whole page?

javascript php jquery html css

I am loading content from an external php file and inserting it into a using query's $.get functionality. The included file contains various css classes. For example, to style hyperlinks. When the...