PHP中文乱码分类及解决办法大全
PHPMYSQL做网站开发通常都会碰到浏览器输出中文字符时乱码,这个问题的原因主要是因为HTML内容编码,PHP文件编码和MySQL数据库编码这三者不一致造成的。下面我们以UTF-8为例简述一下如何统一这三者之间的关系。 新增一个PHP文件,命名为test_charset.php,将
PHP+MYSQL做网站开发通常都会碰到浏览器输出中文字符时乱码,这个问题的原因主要是因为HTML内容编码,PHP文件编码和MySQL数据库编码这三者不一致造成的。下面我们以UTF-8为例简述一下如何统一这三者之间的关系。
新增一个PHP文件,命名为test_charset.php,将下面的代码保存到该文件中:
$charset = "utf8";
$con = mysql_connect("localhost", "root", "");
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $con);
mysql_select_db("ecshop", $con);
$sql = "SELECT user_name, email FROM ecs_admin_user WHERE user_id = 4";
$result = mysql_query($sql, $con);
$array = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_close($con);
$name = $array["user_name"];
$email = $array["email"];
?>
你好, !
你的邮件是:
HTML内容编码
上面的第22行:
这里我们指定了浏览器在解析HTML时使用UTF-8编码 。如果这里没有指定,浏览器会使用自己缺省的编码。不同的浏览器缺省编码会有所不同,比如IE6是GB2312,FireFox是UTF-8。所以,上面这段代码如果没有第22行,在FireFox会显示正常,在IE6就会显示乱码。
PHP文件编码
PHP文件本身也需要有一致的编码。如何检查自己的PHP文件是哪一种编码呢?Windows下简单的可以用记事本来处理。用记事本打开PHP文件,从“文件”菜单中选择“另存为...”
在打开的对话框最下面有一个“编码”的选项,当前看到的就是该文件现在的编码。如果要变为其它的编码,从下拉列表框中选择,然后点击“保存”按钮。
MySQL数据库编码
数据库连接成功后,应该第一时间执行一条设置编码指令,如上面代码的第7行。这里有一点需要特别注意,UTF-8是正常的写法,不过在MySQL中简写成了UTF8,中间没有横线。上面第7行是将连接编码设置成utf8($charset="utf8"),而不是utf-8。这里附上ECShop设置连接编码的完整实现,供大家参考。文件是includes/cls_mysql.php。
function set_mysql_charset($charset)
{
/* 如果mysql 版本是 4.1+ 以上,需要对字符集进行初始化 */
if ($this->version > '4.1')
{
if (in_array(strtolower($charset), array('gbk', 'big5', 'utf-8', 'utf8')))
{
$charset = str_replace('-', '', $charset);
}
if ($charset != 'latin1')
{
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $this->link_id);
}
}
}
原文: http://www.lai18.com/content/312385.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
