When php connects to the mysql database, garbled characters usually appear only in Chinese characters, but not in English, because the default connection of mysql is utf8. If the encoding of your page and the database are not consistent, garbled characters may appear.
Solution
The page is gb2312
The code is as follows
代码如下 |
复制代码 |
mysql_qurey指定数据库的解码为“set names gb2312”是防止中文乱码的。
|
|
Copy code
|
代码如下 |
复制代码 |
mysql_query('SET NAMES utf8');//根据php文件编码设置链接编码
|
Mysql_qurey specifies the decoding of the database as "set names gb2312" to prevent Chinese garbled characters.
代码如下 |
复制代码 |
$connection=mysql_connect ($localhost, $username, $password);
mysql_query('SET NAMES utf8');//根据php文件编码设置链接编码
//gbk的话
//mysql_query('SET NAMES gbk');
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can't use db : ' . mysql_error());
}
|
The page encoding is uft8 |
The code is as follows |
Copy code |
mysql_query('SET NAMES utf8');//Set the link encoding according to the php file encoding
Example
The code is as follows
|
Copy code
|
$connection=mysql_connect ($localhost, $username, $password);
mysql_query('SET NAMES utf8');//Set the link encoding according to the php file encoding
//gbk words
//mysql_query('SET NAMES gbk');
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can't use db : ' . mysql_error());
}
This can solve the problem that the output data will not be garbled. If the data is garbled when saving, please try to make the page encoding consistent with the database field encoding to solve the problem.
http://www.bkjia.com/PHPjc/632172.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632172.htmlTechArticleGarbled characters when php connects to the mysql database are generally Chinese characters, and English will not appear because mysql connects by default It is utf8. If your page and database encodings are not consistent, it is possible...
|
|