简体   繁体   English

php-sqlsrv编码

[英]php - sqlsrv encoding

My Project builds on PHP and connect to MS SQL Server. 我的项目建立在PHP上,并连接到MS SQL Server。 I am using sqlsrv library. 我正在使用sqlsrv库。 The fields type in MS SQL is nvarchar. MS SQL中的字段类型为nvarchar。 When I define the parameters for connection I also put "utf8". 当我定义连接参数时,我还要输入“ utf8”。 It is: 它是:

global $cnf;
$cnf = array();
$cnf["mssql_user"]  = "xxx";
$cnf["mssql_host"]  = "xxx";
$cnf["mssql_pw"]    = "xxx";
$cnf["mssql_db"]    = "xxx";
$cnf["CharacterSet"]    = "**UTF-8**";

When Insert records to database, for Vietnamese content and Chinese content I use: 将记录插入数据库时​​,对于越南语内容和中文内容,我使用:

$city = iconv('UTF-8', 'utf-16le', $post['city']);
$params = array(array($city, null, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)));
$sql= "INSERT INTO tblCityGarden (city) VALUES(?)
$stmt   = sqlsrv_query( $this->dbhandle, $sql, $params);

It inserts data OK for Vietnamese and Chinese language (the data stored in database for Vietnamese and Chinese is correct). 它为越南语和中文插入数据确定(越南语和中文数据库中存储的数据正确)。

However when I load the records back into web, It appears the strange character (?, ). 但是,当我将记录加载回Web时,它会出现一个奇怪的字符(?, )。 I try some php as iconv, mb_detect_encoding, mb_convert_encoding and search many results on internet, but It cannot work. 我尝试将一些php作为iconv,mb_detect_encoding,mb_convert_encoding,并在Internet上搜索许多结果,但是它不起作用。 How can I display correct data 如何显示正确的数据

Please someone who has experiences about this issues 请有经验的人

I had this same problem ( ), but with single quotes, double qoutes, and "Rights Reserved" characters... Here is what I've found: 我遇到了同样的问题( ),但是用单引号,双qoutes和“保留权利”字符...这是我发现的内容:

  • The CharacterSet specified seems to "rule all", so what you set this to will determine the encoding for the connection (as it should). 指定的CharacterSet似乎“全部统治了”,因此将其设置为什么将确定连接的编码(应如此)。 I did not have ANY CharacterSet configured on my connection(s). 我的连接上没有配置任何CharacterSet Simply setting this resolved my issue, along with making sure that the values that were inserted into my DB were not double encoded via htmlspecialchars() . 只需设置此设置即可解决我的问题,并确保插入到数据库中的值未通过htmlspecialchars()进行双重编码。
  • header()'s must be set before ANY output (this is really important) 必须在ANY输出之前设置header()的位置(这很重要)
  • headers cannot be set to something different later in the document 标头无法在文档的后面设置为其他内容
  • Sometimes trailing spaces before and/or after the closing ?> in your PHP file can cause issues (I don't use this closing tag, but I saw this mentioned a lot while searching) 有时在PHP文件中,在结束符之前和/或之后是否在空格后面加上空格?>可能会导致问题(我不使用该结束符,但在搜索时看到的内容很多)

I am not familiar with iconv() , and I am most certainly not experienced with encoding in general, but I solved my issue just by taking the time to check my headers and ensure they meet the above standards... 我不熟悉iconv() ,并且我当然对编码没有经验,但是我花了一些时间检查我的标头并确保它们满足上述标准,从而解决了我的问题...

Your query parameters also look strange: $params = array(array($city, null, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY))); 您的查询参数看起来也很奇怪: $params = array(array($city, null, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)));

I have not seen a multidimensional array passed into that argument... (just a note) 我还没有看到传递给该参数的多维数组...(只是一个注释)

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

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