scw121 发表于 2008-11-7 12:52:49

CI中的编码问题

我用的是phpmyadmin,数据库里是utf8编码,,在控制器下index.php的一个函数读取数据库中的数据


congif文件里确实也设置了utf8,,函数是这样的

function view_date()
   {
         $query = $this->db->query('SELECT * FROM demo_node');
   //echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cn" lang="cn">';
   //echo "<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">";
         foreach($query->result_array() as $row)
   {
      echo $row['id'].'<br>';
   echo $row['name'].'<br>';
   echo $row['title'].'<br>';
   echo $row['remark'].'<br>';
   }

得出来的中文是乱码,怎么弄呢。。

scw121 发表于 2008-11-7 12:56:31

class Blog extends Controller{
          function Blog()
                  {
                     parent::Controller();
             $this->load->helper('url');
                       $this->load->database();
                       mysql_query("set names gb2312");
              }
      function index()
          {
                
                  echo anchor('blog/comments', 'Click Here');
            echo "这是我的博客";
          }
          function hello()
        {
                  echo "hello,world";
          }
          function error()
        {
                  echo "出错了,找不到你请求的页面!";
          }
          function _dispermission()
        {
                  echo "Access Denided!";
          }
          function view_date()
          {
         $query = $this->db->query('SELECT * FROM demo_node');
                   echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cn" lang="cn">';
                  echo "<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">";
         foreach($query->result_array() as $row)
                   {
                      echo $row['id'].'<br>';
                          echo $row['name'].'<br>';
                          echo $row['title'].'<br>';
                          echo $row['remark'].'<br>';
                   }
                   echo '</html>';
          }
          
        /*function _remap($method)
        {
                  if($method=='hello')
                {
                          $this->hello();
                  }
                  else
                {
                          $this->error();
                  }
          }*/


郁闷。我连接数据库后,mysql_query("set names gb2312");就正常了,难道CI不会自动转换嘛,,,

scw121 发表于 2008-11-7 13:03:30

再问个问题,phpmyadmin里的数据是乱码,怎么把他变成中文。。
我记得我插入数据时名名是mysql_query("set names utf-8");而且数据库默认也是utf-8编码,怎么插到里面就成乱码了呢。。。

huanghuibin 发表于 2008-11-7 17:27:44

数据库默认也是utf-8编码?utf8_general_ci吗?

sankai 发表于 2008-11-7 17:51:39

:)你都已經全面UTF8化了,為什麼在網頁的head卻宣告<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">
應該是要變成<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">,我猜這應該是問題的主因

建議所有文件的格式,應該都必需存成 UTF-8 編碼(去掉BOM),才是好習慣:lol

huanghuibin 发表于 2008-11-7 17:55:10

:L :L
那两句楼主注释掉了吧。
//

Hex 发表于 2008-11-7 23:34:51

我看乱码就是 <meta 标签造成的吧。

pisoul 发表于 2008-11-8 00:47:02

注释掉了也会默认浏览GBK的
页: [1]
查看完整版本: CI中的编码问题