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;
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');
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);
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!