Home > Backend Development > PHP Problem > What to do if php sql is garbled

What to do if php sql is garbled

藏色散人
Release: 2023-03-17 11:26:01
Original
2060 people have browsed it

Solution to php sql garbled code: 1. Select ANSI encoding when saving the PHP file; 2. Add "header("Content-Type: text/html; CHARSET=GBK");" to the PHP file header; 3. Transcode the SQL before querying; 4. Just transcode the query results containing Chinese columns.

What to do if php sql is garbled

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

What should I do if php sql is garbled?

Solve the problem of PHP connection to SQLSERVER and Chinese garbled characters

1. Chinese characters in the SQL statement will cause the query to fail;

2.Query The result will be garbled characters in Chinese.

Solution one (simpler, recommended):

1. Select ANSI encoding when saving the PHP file;

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

2, add

header("Content-Type: text/html; CHARSET=GBK");
Copy after login
## to the PHP file header

#Solution 2 (more troublesome):

1. Keep the default UTF-8 encoding for PHP files;

2. Code SQL before querying Transcoding

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

3. Query results for transcoding columns containing Chinese

$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 />";
    }
}
Copy after login
Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What to do if php sql is garbled. 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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template