教育行業(yè)A股IPO第一股(股票代碼 003032)

全國(guó)咨詢/投訴熱線:400-618-4000

寫給PHP新人,如何避免亂碼,告訴你四種常用方法

更新時(shí)間:2016年09月14日17時(shí)20分 來源:傳智播客PHP培訓(xùn)學(xué)院 瀏覽次數(shù):

在我們每個(gè)人在開始學(xué)習(xí)php的時(shí)候,都經(jīng)歷過一個(gè)痛苦的亂碼過程,手足無措?,F(xiàn)在告訴大家五種方法,徹底避免亂碼。

第一類:文件亂碼

沒出指定編碼, 但目前瀏覽器有的是默認(rèn)gbk,有的卻是默認(rèn)是utf8,這就導(dǎo)致亂碼!

解決辦法:

       1、 入口文件的第一行 ,強(qiáng)制輸出常用)!

<?php
       header("Content-type: text/html; charset=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)建的時(shí)候指定編碼常用)

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 '是否顯示,默認(rèn)顯示',
        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}");



本文版權(quán)歸傳智播客PHP培訓(xùn)學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明作者出處,謝謝!
作者:傳智播客PHP培訓(xùn)學(xué)院
首發(fā):http://www.xamj520.com/php/index.shtml

0 分享到:
和我們?cè)诰€交談!