When using PHP to connect to MySQL, you often encounter the problem of garbled characters. This is a big problem for us when writing multi-language websites. Before dealing with this problem, we need to first understand the reasons for the formation of garbled characters.
1. Reasons for garbled characters
When using PHP to connect to MySQL, if the character set of MySQL is inconsistent with the character set of PHP, garbled characters will appear. The specific reasons are as follows:
- MySQL’s character set is inconsistent with PHP’s character set
MySQL’s default character set is UTF-8, while PHP’s default character set is ISO-8859-1. If you do not specify a character set in your PHP code, PHP will use the ISO-8859-1 character set. At this time, if the data queried from MySQL contains UTF-8 characters, garbled characters will appear.
- The character set of the MySQL data table is inconsistent with the character set of the MySQL server
In MySQL, each data table has a default character set. If the character set of the data table is inconsistent with the character set of the MySQL server, garbled characters may appear.
- The way PHP connects to MySQL is incorrect
When connecting to MySQL, PHP has a variety of connection methods, such as mysql_connect, mysqli_connect, PDO, etc. Different connection methods will use different encoding methods. If selected improperly, it will lead to garbled code problems.
2. Solve the garbled code problem
In response to the above reasons, the following measures can be taken to solve the garbled code problem.
- Specify character set for PHP
In PHP code, solve the problem of garbled characters by specifying the character set. You can use the following code:
header('Content-Type:text/html;charset=utf-8');
Copy after login
- Modify the MySQL default character set
You can modify the MySQL default character set in the MySQL configuration file my.cnf. Open the file, find the [mysqld] section, and add the following code:
character-set-server=utf8
Copy after login
- Modify the character set of the MySQL data table
You can modify the character set of the data table through the following statement:
ALTER TABLE {table_name} CONVERT TO CHARACTER SET utf8;
Copy after login
- Modify the way PHP connects to MySQL
You can use PDO to connect to MySQL and specify the character set when connecting, as follows:
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'password');
Copy after login
Use mysqli When connecting to MySQL, you can specify the character set through the following code:
$mysqli = mysqli_connect('localhost', 'root', 'password', 'test');
mysqli_set_charset($mysqli, 'utf8');
Copy after login
- Save the PHP file as UTF-8 encoding format
If your PHP file contains Chinese characters, then save the file Be sure to save the file in UTF-8 encoding format, otherwise garbled characters will appear.
To sum up, the garbled problem of PHP connection to MySQL is a very common problem, but as long as you master the solution, you can easily deal with it. I hope this article can help everyone.
The above is the detailed content of What is the reason for the garbled code problem when php connects to mysql? How to deal with it?. For more information, please follow other related articles on the PHP Chinese website!