Home >Backend Development >PHP Problem >What should I do if the php link to sqlserver is garbled in Chinese?

What should I do if the php link to sqlserver is garbled in Chinese?

coldplay.xixi
coldplay.xixiOriginal
2020-07-10 13:06:562849browse

Solution to the Chinese garbled code in php link sqlserver: 1. Open the query analyzer of sqlserver2005; 2. Open [php.ini] and configure [mssql.charset = "utf-8"]; 3. In php Add relevant codes to the file; 4. Transcode the input data.

What should I do if the php link to sqlserver is garbled in Chinese?

Solution to Chinese garbled php link to sqlserver:

First, open the query analysis of sqlserver2005 Run the server , code

SELECT  COLLATIONPROPERTY('Chinese_PRC_Stroke_CI_AI_KS_WS', 'CodePage')

, and check the result. "936" is displayed, indicating that the encoding of the database is GBK

. The attached table is as follows:

936 Simplified Chinese GBK

950 Traditional Chinese BIG5

437 United States/Canada English

932 Japanese

949 Korean 8

66 Russian

65001 unicode UFT-8

Second, open php.ini and configure it as follows

mssql.charset = "utf-8"

Remember to restart the server! ! ! ! ! ! !

Third, add the following code in the php file,

<?php
   ……   header("content-Type: text/html; charset=utf-8");
   ……?>

Fourth, transcode the input data

The data table test is as follows:

What should I do if the php link to sqlserver is garbled in Chinese?

The variable submitted by the simulation is type = 'Unicom', and it is transcoded

$type = &#39;联通&#39;;$type = (iconv(&#39;UTF-8&#39;,&#39;GBK&#39;,$type));

sql statement is as follows,

$result= mssql_query("select * from dbo.test where type = &#39;$type&#39; and name = &#39;TOM&#39;", $conn);

Fifth, transcode the query results (the core code is as follows)

$res[&#39;type&#39;] = iconv(&#39;GBK&#39;,&#39;UTF-8&#39;,$result[&#39;type&#39;]);echo $res[&#39;type&#39;];

View the output:

China Unicom

it shows good.

Sixth, change the fifth code as follows:

$res[&#39;stname&#39;] = urlencode(iconv(&#39;GBK&#39;,&#39;UTF-8&#39;,$row[&#39;stname&#39;]));

Then, encapsulate JSON and send it out

echo urldecode(json_encode($res));

Related Learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What should I do if the php link to sqlserver is garbled in Chinese?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn