PHP写入数据库中文乱码问题

声明:本篇文章来自http://www.jb51.net/article/30123.htm

PHP页面转UTF-8编码问题

1.在代码开始出加入一行:

header("Content-Type: text/html;charset=utf-8"); 
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">2.PHP文件编码问题 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">如果是ANSI,需要将编码改成:UTF-8。 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">3.PHP文件头BOM问题: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">PHP文件一定不可以有BOM标签 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">否则,会出现session不能使用的情况,并有类似的提示: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">这是因为,在执行session_start() 的时候,整个页面不能有输出,但是当由于前PHP页面存在BOM标签, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">PHP把这个BOM标签当成是输出了,所以就出错了! </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">所以PHP页面一定要删除BOM标签 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">删除这个BOM标签的方法: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">1.可以用Dreamweaver打开文件,并重新保存,即可以去除BOM标签! </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">2.可以用EditPlus打开文件,并在菜单“首选项”->“文件”->"UTF-8标识",设置为:“总是删除签名”, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">然后保存文件,即可以去除BOM标签! </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">4.PHP以附件形式保存文件的时候,UTF-8编码问题: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">PHP以附件形式保存文件,文件名必须是GB2312编码, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">否则,如果文件名中有中文的话,将是显示乱码: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">如果你的PHP本身是UTF-8编码格式的文件, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">需要将文件名变量由UTF-8转成GB2312: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">iconv("UTF-8", "GB2312", "$filename"); </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">利用程序来实例字符截取方法 </span>
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="php">function utf8_substr($str,$len)
{
  for($i=0;$i<$len;$i++)
  {
    $temp_str=substr($str,0,1);
    if(ord($temp_str) > 127){
      $i++;
    if($i<$len){
      $new_str[]=substr($str,0,3);
      $str=substr($str,3);
      }
    }else {
    $new_str[]=substr($str,0,1);
    $str=substr($str,1);
    }
  }
  return join($new_str);
}

MYSQL数据库使用UTF-8编码的问题

1.用phpmyadmin创建数据库和数据表

创建数据库的时候,请将“整理”设置为:“utf8_general_ci”

或执行语句:


<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="php">CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">创建数据表的时候:如果是该字段是存放中文的话,则需要将“整理”设置为:“utf8_general_ci”, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">如果该字段是存放英文或数字的话,默认就可以了。 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">相应的SQL语句,例如: </span>
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="sql">CREATE TABLE `test` (
`id` INT NOT NULL ,
`name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ; 

2.用PHP读写数据库

在连接数据库之后:


<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="php">$connection = mysql_connect($host_name, $host_user, $host_pass); 

加入两行:


<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span></span><pre name="code" class="php">mysql_query("set character set 'utf8'");//读库
mysql_query("set names 'utf8'");//写库
//其实读写都可以只加入
mysql_query("set names 'utf8'")

就可以正常的读写MYSQL数据库了。

用的appserv-win32-2.5.10做的环境,装这个包的时候用默认的utf8编码。

在写数据库连接文件时,写成:


<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span></span></span><pre name="code" class="php">$conn = mysql_connect("$host","$user","$password");
mysql_query("SET NAMES 'UTF8'");
mysql_select_db("$database",$conn); 

然后在做页面时,注意这句:


<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px; background-color: rgb(221, 237, 251);"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </span></span></span></span>
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">这样不管输入数据库的中文,还是页面显示,就都正常了。 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">在DW CS4版里,默认生成的也是utf8页面。 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">同样的,如果一开始写数据库连接文件时写成: </span></span></span></span><pre name="code" class="php">mysql_query("SET NAMES 'GBK'"); 

那页面也要相应变成:


<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span></span></span><pre name="code" class="cpp"><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 

<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">

</span>
				
时间: 2024-07-31 14:30:24

PHP写入数据库中文乱码问题的相关文章

C#写入Oracle 中文乱码问题

这个问题是我刚踏入工作觉得最坑的一个问题,找了很多方法.也问过不少人,但还是没能解决,偶然间返现了新大陆.... 具体问题描述是这样的: 我可以读取Oracle数据库中已有的中文内容,并能正确显示(Oracle中的中文通过SQLplus录入),但当我使用C#程序插入中文记录时,发现数据库中显示为乱码,读取出来也为乱码.我试了很多种编码方式,问题都不能解决. 以下是查找相关资料得到的解决办法: 主要问题是: oracle客户端软件的字符编码与服务器端的字符编码不一致造成的. oracle的字符编码

写入MySQL中文乱码问题

相信使用数据库进行存储的大家都遇到过中文乱码问题,如何彻底解决?我百度了很多资料与博客,想把自己的经历总结起来给大家参考一下,接下来我先罗列一下大部分修改乱码问题的方法: 1.   修改MySQL数据库的整体编码 引用自:http://www.cnblogs.com/24la/p/update-mysql-default-character.html 查看方式数据库编码: show variables like 'character%'; 该图是复制的(我的已经改好了) 出现上图情况时,可以进行

Net、c# 连接Mysql数据库中文乱码

网上有两种解决方案: 第一种是,每次执行语句的时候都和PHP的类似,先执行 一句“set names utf8”或者“set names gb2312”; 1 MySQLCommand setformat = new MySQLCommand("set names b2312",m_Connection); 2 setformat.ExecuteNonQuery(); 3 setformat.Dispose(); 第二种是,在webconfig里加一句“Charset=gbk”; 1

python读取数据库中文乱码问题

今天朋友遇到过怪问题,在同一个页面显示的2条中文记录一个正常,一个乱码,2条记录分别从不同的表里取出.录入的时候采用直接录入.仔细观察2者区别,发现能正常显示的字段在表中类型为nvarchar,不能的是varchar,试着将其改成nvarchar,问题解决. 因为对sqlserver不熟悉.通过查询手册得知: nvarchar表示以Unicode格式存储可变长度的 数据,所以能显示中文,而varchar是用非unicode存储数据,所以乱码.将Varchar类型设置为nvarchar类型,发现问

Windows使用MySQL数据库中文乱码问题

声明:本文关于MySQL中文乱码问题的解决方案均基于Windows 10操作系统,如果是Linux系统会有较多不适用之处,请谨慎参考. 一.MySQL中文乱码情况 1. sqlDevelpor MySQL客户端中文乱码 sqlDevelopor操作MySQL中文乱码 2. command-line MySQL客户端中文乱码 控制台操作MySQL中文乱码 二.MySQL中文乱码产生原因 Windwos中文系统默认的字符编码集是gbk(扩展国标码,包括简体中文.繁体中文.朝鲜语.日本语等东亚语言),

Oracle数据库中文乱码问题

最近碰到Oracle乱码问题,刚开始甚是头疼,以前在合肥出差的时候,这种问题也碰到过,当时直接抛给了“乌压压一片”(一个搞数据的同事儿),这次没办法躲过,只好硬着头皮上.虽然我这次碰到的是Oracle乱码问题中的一个,但是我决定将这个乱码问题整理清楚(不整清楚,就觉得身边有个定时炸弹,怕下次整数据库的时候会突然又爆炸). 解决这个问题的关键在于理解字符集的概念,所以在正文开始之前,有必要先提一下字符集的相关知识!(这部分知识,对于解决j2ee中文参数传递过程中出现的乱码,也非常具有参考意义) 一

MySQL数据库中文乱码问题

mysql> select * from books; +-----+---------------------------------+---------+-------------+-------+------------+--------+-------------+ | bId | bName | bTypeId | publishing | price | pubDate | author | ISBN | +-----+--------------------------------

关于Androdi中SQLITE 3采用GBK编码存储,数据库中文乱码问题。

1.最近开发一个项目,用SQLite Expert Personal打开数据库如下图,title会产生乱码,问题. 2.由于SQL lite默认是存储UTF-8格式,后来更改数据库编码类型为ANSI,依据操作系统,本地ANSI为GB2312格式,查看发现编码格式正常. 3.用Android程序读取,采用以下方式,可以完整读取出中文字符. Product pr=new Product(); //解决中文乱码问题 byte[] val = cursor.getBlob(cursor.getColum

PL/sql developer连接数据库的问题以及oracle数据库中文乱码的问题

今天第二次配置PL/sql developer,表示很蛋疼,昨天因为动了一个东西然后莫名其妙的就再也连接不了数据库,总是显示各种错误,我动的东西是因为中文会显示乱码,(因为我是用32位的PL/sql developer连接64位的orcale,所以参照网上的教程在添加了一个环境变量名字是NLS_LANG,值是SIMPLIFIED CHINESE_CHINA.ZHS16GBK,然后把注册表下面的这个名字的值也改成了这一样的,然后我再次登陆就显示各种错误了,连改回去都不行,还尝试过重新创建数据库,最