Home > Database > Mysql Tutorial > How to solve php mysql query results showing garbled characters

How to solve php mysql query results showing garbled characters

WBOY
Release: 2023-05-27 08:10:42
forward
1185 people have browsed it

1. Inconsistent character encoding

The default character set when MySQL is installed is latin1, and many websites use UTF-8 encoding, which may cause queries The result is garbled characters. You can use the following command to modify the MySQL encoding:

ALTER DATABASE your_database_name CHARACTER SET utf8;
ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8;
Copy after login

where your_database_name and your_table_name are replaced with your database and data table names respectively.

2. Database and page encodings are inconsistent

Establishing a database connection without specifying a character set may cause the query results to use different character sets and eventually appear garbled. You can use the following command to specify the character set when connecting:

mysqli_set_charset($link, 'utf8');
Copy after login

where $link is the variable name of the database connection, which should be modified according to the actual situation.

3. PHP version is incompatible

In PHP5.5 and below, the mysql_ function is abandoned and it is recommended to use mysqli_ or PDO function. If you are using the mysql_ function and running it in PHP7 or above, the query results may be garbled. It is recommended to upgrade to mysqli_ or PDO function.

4. Convert character set

Use the iconv() function in PHP to convert characters from one encoding to another. For example, convert GBK encoding to UTF-8 encoding:

$content = iconv('GBK', 'UTF-8', $content);
Copy after login

The above is the detailed content of How to solve php mysql query results showing garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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