亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP如何解決亂碼的四種方法

原創(chuàng) 2017-01-03 14:38:06 420
摘要:在我們每個人在開始學(xué)習(xí)php的時候,都經(jīng)歷過一個痛苦的亂碼過程,手足無措?,F(xiàn)在告訴大家四種方法,徹底避免亂碼。       第一類:文件亂碼        沒出指定編碼, 但目前瀏覽器有的是默認gbk,有的卻是默認是utf8,這就導(dǎo)致亂碼!     

在我們每個人在開始學(xué)習(xí)php的時候,都經(jīng)歷過一個痛苦的亂碼過程,手足無措?,F(xiàn)在告訴大家四種方法,徹底避免亂碼。
       第一類:文件亂碼
        沒出指定編碼, 但目前瀏覽器有的是默認gbk,有的卻是默認是utf8,這就導(dǎo)致亂碼! 
         解決辦法:
       1、 入口文件的第一行 ,強制輸出(常用)!

<?php
                header("Content-type: text/html; charst=utf-8");

      2、 html模版head部分(常用)!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

          第二類 數(shù)據(jù)庫亂碼

      1、數(shù)據(jù)庫本身創(chuàng)建的時候指定編碼(常用)

create table cz_category(
        cat_id smallint unsigned not null auto_increment primary key comment '商品類別ID',
        cat_name varchar(30) not null default '' comment '商品類別名稱',
        parent_id smallint unsigned not null default 0 comment '商品類別父ID',
        cat_desc varchar(255) not null default '' comment '商品類別描述',
        sort_order tinyint not null default 50 comment '排序依據(jù)',
        unit varchar(15) not null default '' comment '單位',
        is_show tinyint not null default 1 comment '是否顯示,默認顯示',
        index pid(parent_id)
)engine=MyISAM charset=utf8;

      如果是在phpmyadmin下創(chuàng)建的,那么就應(yīng)該 

         2、 php連接mysql(常用)

<?php
function getField($table_name)
{
$link = mysql_connect('localhost','root','1234abcd');
mysql_query('use daxue');
mysql_query('set names utf8');
$result=mysql_query("desc {$table_name}");

更多關(guān)于PHP如何解決亂碼的方法請關(guān)注PHP中文網(wǎng)(ipnx.cn)其它文章!

發(fā)佈手記

熱門詞條