PHP connection to MySQL database Chinese garbled solution_PHP tutorial

WBOY
Release: 2016-07-13 10:56:27
Original
1036 people have browsed it

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...
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!