What to do if PHP SQLSERVER Chinese garbled characters

藏色散人
Release: 2023-03-01 07:04:01
Original
3999 people have browsed it

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");
Copy after login

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);
Copy after login

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])."
"; } }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!