Fanda
Fanda

Reputation: 3786

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)

Upvotes: 1

Views: 6360

Answers (2)

Fanda
Fanda

Reputation: 3786

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);

Upvotes: 1

Basic Intent
Basic Intent

Reputation: 11

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

Upvotes: 0

Related Questions