Home  >  Article  >  Backend Development  >  What to do if PHP SQLSERVER Chinese garbled characters

What to do if PHP SQLSERVER Chinese garbled characters

藏色散人
藏色散人Original
2020-05-10 11:38:354279browse

What to do if PHP SQLSERVER Chinese garbled characters

What to do if PHP SQLSERVER has Chinese garbled characters?

PHP connection to SQLSERVER Chinese garbled problem

1. The presence of Chinese in the SQL statement will cause the query to fail;

2. The query result is Chinese will display garbled characters.

Solution 1 (simpler, recommended):

1. Select ANSI encoding when saving the PHP file;

Attachment: VS Code changes the default text encoding, File->Preferences->Usersettings, search for encoding, change utf8 to gbk

2, add PHP file header

header("Content-Type: text/html; CHARSET=GBK");

Solution 2 (more troublesome):

1. Keep the PHP file in the default UTF-8 encoding;

2. Convert SQL before querying

$sql = "SELECT '是'='是'"; 
$sql=iconv('UTF-8','GBK',$sql);

3. Convert query results containing Chinese columns

$stmt = sqlsrv_query( $conn, $sql);
if($stmt){
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
        echo iconv(&#39;GBK&#39;,&#39;UTF-8&#39;,$row[0])."<br />";
    }
}

Recommended: "PHP Video Tutorial"

The above is the detailed content of What to do if PHP SQLSERVER Chinese garbled characters. 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