PHP和Mysql中转UTF8编码问题汇总,mysql中转utf8编码
一个网站如果需要国际化,就需要将编码从GB2312转成UTF-8,其中有很多的问题需要注意,如果没有转换彻底,将会有很多的编码问题出现!
PHP页面转UTF-8编码问题
1.在代码开始出加入一行: header("Content-Type: text/html;charset=utf-8");
2.PHP文件编码问题 点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,如果是ANSI,需要将编码改成:UTF-8。
3.PHP文件头BOM问题: PHP文件一定不可以有BOM标签,否则,会出现session不能使用的情况,并有类似的提示: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent 这是因为,在执行session_start() 的时候,整个页面不能有输出,但是当由于前PHP页面存在BOM标签,PHP把这个BOM标签当成是输出了,所以就出错了! 所以PHP页面一定要删除BOM标签
删除这个BOM标签的方法:
1.可以用Dreamweaver打开文件,并重新保存,即可以去除BOM标签!
2.可以用EditPlus打开文件,并在菜单“首选项”->“文件”->"UTF-8标识",设置为:“总是删除签名”,然后保存文件,即可以去除BOM标签!
3.PHP以附件形式保存文件的时候,UTF-8编码问题: PHP以附件形式保存文件,文件名必须是GB2312编码,否则,如果文件名中有中文的话,将是显示乱码: 如果你的PHP本身是UTF-8编码格式的文件,需要将文件名变量由UTF-8转成GB2312: iconv("UTF-8", "GB2312", "$filename");
4.截断显示文章标题时,出现乱码或者“?”问号的问题:
一般文章标题很长的时候,会显示一部分标题,会对文章标题进行截断,由于一个UTF-8编码格式的中文字符会占用3个字符宽度,截取标题的时候,有时会只截取到一个中文字符的1个字符或2字符宽度,没截取完整,将出现乱码或“?”问号的情况,用下面的函数截取标题,就不会有问题:
function get_brief_str($str, $max_length) { echo strlen($str) . ""; if (strlen($str) > $max_length) { $check_num = 0; for ($i = 0; $i < $max_length; $i++) { if (ord($str[$i]) > 128) $check_num++; } if ($check_num % 3 == 0) $str = substr($str, 0, $max_length) . "..."; else if ($check_num % 3 == 1) $str = substr($str, 0, $max_length +2) . "..."; else if ($check_num % 3 == 2) $str = substr($str, 0, $max_length +1) . "..."; } return $str; }
MYSQL数据库使用UTF-8编码的问题
1.用phpmyadmin创建数据库和数据表 创建数据库的时候,请将“整理”设置为:“utf8_general_ci”或执行语句:
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
创建数据表的时候:如果是该字段是存放中文的话,则需要将“整理”设置为:“utf8_general_ci”,如果该字段是存放英文或数字的话,默认就可以了。
相应的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读写数据库
在连接数据库之后:
$connection = mysql_connect($host_name, $host_user, $host_pass);
加入两行:
mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库
希望本文所述对大家的PHP+MySQL程序设计有所帮助。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
