默生 发表于 2008-6-10 15:39:46

[求助]取数据库的数据出现乱码!

我在application/config/database.php加了:
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
我用的就是UTF8.
模型:class books_model extends Model
{

      function __construct(){
                parent::Model();
      }

      /*
      function get_books($num, $offset) {
                $query = $this->db->get('christian_books', $num, $offset);

                return $query;
      }*/
      function get_pro_books()
      {      
                  //加上下面这句就不会有乱码了,
                  $this->db->query('SET NAMES utf8');
                $query=$this->db->query('SELECT * FROM book');

               if ($query->num_rows() > 0)
                {
                   $rows= $query->result();
                }

      return $rows;
      }
}
?>
控制器:

class Books extends Controller {

      function __construct() {
                parent::Controller();
                $this->load->helper('url');
                $this->load->database();
      }

      function index() {

                //load the model and get results
                $this->load->model('books_model');

                $data['results'] = $this->books_model->get_pro_books();
                $data['hi']="这里的汉字没有错!";

                // load the view
                $this->load->view('books_view', $data);
      }

}
lNew Page | xHTML1.0 Transitional?>
视图:

<html>
<head>
</head>
<body>

<?php foreach($results as $row):?>
    <?=$row->author?>
<?php endforeach;?>
<?=$hi?>
</body>
</html>

出来的结果是???????????????????这里的汉字没有错!

[ 本帖最后由 默生 于 2008-6-13 10:03 编辑 ]

Hex 发表于 2008-6-10 15:54:44

HTML 也要使用 UTF-8编码,并用UTF-8编码保存文件。

默生 发表于 2008-6-10 16:31:28

原帖由 Hex 于 2008-6-10 15:54 发表 http://codeigniter.org.cn/forums/images/common/back.gif
HTML 也要使用 UTF-8编码,并用UTF-8编码保存文件。
我的是UTF8,

<!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" xml:lang="zh" lang="zh" dir="ltr">
<head profile="http://www.w3.org/2000/08/w3c-synd/#">
    <meta http-equiv="Content-Type" c />
    <meta name="keywords" c />
    <meta name="DEscription" c />
    <meta name="Author" c />

    <title>New Page | xHTML1.0 Transitional</title>
    <link rel="shortcut icon" href="favicon.ico" />
    <!--CSS&JS is here
    <link rel="stylesheet" type="text/css" href="css.css" />
    <script type="text/javascript" src="js.js"></script>
    -->
</head>
<body>
<?php foreach($results as $row):?>
<?=$row->book_author?>
<?php endforeach;?>
<?=$hi?>

</body>
</html>

换成下面这文档头:

<!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" c />
<title>无标题文档</title>
</head>
<body>
<?php foreach($results->result() as $row):?>
<?=$row->book_publisher_year?>
<?php endforeach;?>
<?=$hi?>

</body>
</html>



[ 本帖最后由 默生 于 2008-6-10 17:42 编辑 ]

默生 发表于 2008-6-10 17:01:37

我刚才又试了,插入也是乱码!料到如此!
怎么解决?:Q

itlong 发表于 2008-6-10 18:16:32

这小个还不懂什么叫UTF-8文件。。
你HTML的<meta....>里面是UTF-8了,可是你本身这个view文件保存的时候不是用utf-8格式,建议你用windows自带的笔记本编辑器打开》》另存》》在对话框里选取utf-8格式,不要用什么ASCI格式了。

默生 发表于 2008-6-11 09:31:29

原帖由 itlong 于 2008-6-10 18:16 发表 http://codeigniter.org.cn/forums/images/common/back.gif
这小个还不懂什么叫UTF-8文件。。
你HTML的里面是UTF-8了,可是你本身这个view文件保存的时候不是用utf-8格式,建议你用windows自带的笔记本编辑器打开》》另存》》在对话框里选取utf-8格式,不要用什么ASCI格式了。 ...

谢谢你的回答.

我的确是存为UTF8的,为了说明是在取数据库出的问题,我在model 里面设了一个变量,装了一些汉字,"这里的汉字没有错!"

???????????????????这里的汉字没有错!
正确显示了的,前面的问号是数据库取来的

难道就没有人遇到这样的问题吗?

[ 本帖最后由 默生 于 2008-6-11 09:33 编辑 ]

sdlywwb 发表于 2008-6-11 09:43:57

我昨天遇到这个情况已经解决了

把database.php里面的
$db['default']['char_set'] = "gb2312";
$db['default']['dbcollat'] = "gb2312_chinese_ci";

数据库中都设置成gb2312

谁有好用的验证码共享下!急需

nase 发表于 2008-6-11 15:34:19

http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html

zhuqx1130 发表于 2008-8-24 21:30:14

我是插入中文数据时数据库里显示乱码,但取出来的数据显示在页面是正常的

szlinz 发表于 2008-9-20 10:31:05

UTF-8对中文的支持就是不爽,经常遇到各种各样的乱码错误。
页: [1] 2
查看完整版本: [求助]取数据库的数据出现乱码!