@[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