CodeIgniter 中文亂碼問題

1. HTMLphp

In the html, add the meta-tag:html

<meta charset="utf-8" />mysql

<?php
class home extends CI_Controller {
    function index()
    {
         //    經過經常使用的 php 的 header 函數設置編碼
         header("Content-type:text/html;charset=utf-8");
         echo '測試輸出';
    }

    function myecho()
    {
        //   使用Output類來解決
        $this->output->set_content_type('application/html;charset=utf-8');
        $this->output->set_output("測試輸出");
    }
}
?>

 

2. VI、notepad++等編輯器設置爲utf8sql

  VI:   set enc=utf-8數據庫

 

3. php.iniapp

  default_charset = "utf-8"編輯器

 

4. 數據庫方面函數

4.1  vi /etc/my.cnf測試

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
default-character-set=utf8

4.2 建立表的時候設置默認字符集this

CREATE TABLE news (
  id int(11) NOT NULL AUTO_INCREMENT,
  title varchar(128) NOT NULL,
  slug varchar(128) NOT NULL,
  text text NOT NULL,
  PRIMARY KEY (id),
  KEY slug (slug)
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

4.2  設置 CI 配置: vi application/config/database.php

$db['default']['char_set'] = 'utf8';
相關文章
相關標籤/搜索