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.
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");
$sql = "SELECT '是'='是'"; $sql=iconv('UTF-8','GBK',$sql);
$stmt = sqlsrv_query( $conn, $sql); if($stmt){ while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) { echo iconv('GBK','UTF-8',$row[0])."<br />"; } }
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!