Home > Article > Backend Development > 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('GBK','UTF-8',$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!