PHP--Solve PHP connection sqlserver2005 Chinese garbled problem (with detailed solution)

Source: Internet
Author: User

@[email protected]~~

--php5.2

--phpstudy

--apache

--sqlserver2005

@[email protected]~~ Problem Description

Problem One: PHP connection sqlsever2005, input Chinese, and then query SQL Server corresponding data, because the submission of Chinese is UTF-8, and SQL Server's Chinese is GBK, so the field does not match, there is no query results.

Problem two, PHP connection sqlsever2005, read data table data, because the Chinese is UTF-8, and SQL Server's Chinese is GBK, read out the characters displayed garbled.

@[email protected]** Start solving the problem (#_ #) ~

First, open sqlserver2005 's Query Analyzer, code

SELECT  Collationproperty ('chinese_prc_stroke_ci_ai_ks_ws'CodePage' )

Run, view the results, display "936", indicating that the database is encoded as GBK

^_^ schedule, as follows:

936950437 USA/932
94986665001 Unicode uft-8

Second, open php.ini, configured as follows

" Utf-8 "

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

Third, add the following code to the PHP file,

<? php   ...    Header ("content-type:text/html; charset=utf-8");   ...? >

Finally, transcoding work for input data

The data table test is as follows:

Id Name Type
1 TOM Unicom
2 JERRY Move

The simulated commit variable is type = ' Unicom ', which is transcoded

$type = ' Unicom '; $type = (iconv(' UTF-8 ', ' GB2312 ',$type));

The SQL statements are as follows

$result= Mssql_query ("select * from dbo.test where type = '$type$conn);

Five, the results of the query after transcoding (the core code is as follows)

$res Iconv (' GB2312 ', ' UTF-8 ',$result[' type ']); Echo $res

View the output,

Unicom

Display is normal.

!!! If you need to encapsulate the query results into JSON, and ensure that the sent JSON Chinese encoding is normal!

------Please take a look at the last six, if you do not need, you can skip the following steps.

Six, change the code for the fifth to be adjusted as follows:

$res UrlEncode (iconv(' GB2312 ', ' UTF-8 ',$row

Then, encapsulate the JSON and send it out

Echo UrlDecode (Json_encode ($res));

@[email protected]~ Finish

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.