php乱码

在我们每个人在开始学习php的时候,都经历过一个痛苦的乱码过程,手足无措。现在告诉大家四种方法,彻底避免乱码。

第一类:文件乱码

没出指定编码, 但目前浏览器有的是默认gbk,有的却是默认是utf8,这就导致乱码! 

  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>

第二类 数据库乱码

      1、数据库本身创建的时候指定编码(常用)

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 '排序依据',
        unit varchar(15) not null default '' comment '单位',
        is_show tinyint not null default 1 comment '是否显示,默认显示',
        index pid(parent_id)
)engine=MyISAM charset=utf8;


      如果是在phpmyadmin下创建的,那么就应该

       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}");
原文地址:https://www.cnblogs.com/healy/p/6900584.html