简体   繁体   English

PHP sqlsrv 2和UTF-8

[英]PHP sqlsrv 2 and UTF-8

please, what should I do to store data with UTF-8 into SQL Server (2005) right? 请问,我应该怎么做才能将UTF-8数据存储到SQL Server(2005)中呢? 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 '????????? 我的html页是UTF-8编码的,表中的列是nvarchar类型,连接字符集设置为UTF-8,但是俄语字符串ЛогистикаДругое存储为'????????? ??????' ??????' into table. 进桌子。 I am using sqlsrv v2. 我正在使用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) (php 5.2.17,iis7.5,ms sql server 2005,sqlsrv 2)

It was caused by historical reason, when we used odbc without binding params: 这是由历史原因引起的,当时我们使用了不带绑定参数的odbc:

$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. 存储数据的列必须具有Unicode排序规则,否则它将不接受Unicode字符,从而产生问号。

Using UTF-16 in your database may solve your problem. 在数据库中使用UTF-16可能会解决您的问题。 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 有关更深入的原因,请参阅: http : //blogs.msdn.com/b/qingsongyao/archive/2009/04/10/sql-server-and-utf-8-encoding-1-true-or -false.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM