默生 2008-6-10 15:39
[求助]取数据库的数据出现乱码!
我在application/config/database.php加了:
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
我用的就是UTF8.
模型:[quote]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()
{
//加上下面这句就不会有乱码了,
[color=Red] $this->db->query('SET NAMES utf8');[/color]
$query=$this->db->query('SELECT * FROM book');
if ($query->num_rows() > 0)
{
$rows= $query->result();
}
return $rows;
}
}
?>[/quote]
控制器:
[quote]
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?>[/quote]
视图:
[quote]
<html>
<head>
</head>
<body>
<?php foreach($results as $row):?>
<?=$row->author?>
<?php endforeach;?>
<?=$hi?>
</body>
</html>
[/quote]
出来的结果是[quote]???????????????????这里的汉字没有错![/quote]
[[i] 本帖最后由 默生 于 2008-6-13 10:03 编辑 [/i]]
Hex 2008-6-10 15:54
HTML 也要使用 UTF-8 编码,并用 UTF-8 编码保存文件。
默生 2008-6-10 16:31
[quote]原帖由 [i]Hex[/i] 于 2008-6-10 15:54 发表 [url=http://codeigniter.org.cn/forums/redirect.php?goto=findpost&pid=3704&ptid=669][img]http://codeigniter.org.cn/forums/images/common/back.gif[/img][/url]
HTML 也要使用 UTF-8 编码,并用 UTF-8 编码保存文件。 [/quote]
我的是UTF8,
[quote]
<!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>
[/quote]
换成下面这文档头:
[php]
<!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>
[/php]
[[i] 本帖最后由 默生 于 2008-6-10 17:42 编辑 [/i]]
默生 2008-6-10 17:01
我刚才又试了,插入也是乱码!料到如此!
怎么解决?:Q
itlong 2008-6-10 18:16
这小个还不懂什么叫UTF-8文件。。
你HTML的<meta....>里面是UTF-8了,可是你本身这个view文件保存的时候不是用utf-8格式,建议你用windows自带的笔记本编辑器打开》》另存》》在对话框里选取utf-8格式,不要用什么ASCI格式了。
默生 2008-6-11 09:31
[quote]原帖由 [i]itlong[/i] 于 2008-6-10 18:16 发表 [url=http://codeigniter.org.cn/forums/redirect.php?goto=findpost&pid=3713&ptid=669][img]http://codeigniter.org.cn/forums/images/common/back.gif[/img][/url]
这小个还不懂什么叫UTF-8文件。。
你HTML的里面是UTF-8了,可是你本身这个view文件保存的时候不是用utf-8格式,建议你用windows自带的笔记本编辑器打开》》另存》》在对话框里选取utf-8格式,不要用什么ASCI格式了。 ... [/quote]
谢谢你的回答.
我的确是存为UTF8的,为了说明是在取数据库出的问题,我在model 里面设了一个变量,装了一些汉字,"[color=Red]这里的汉字没有错![/color]"
[quote]
???????????????????这里的汉字没有错!
[/quote]正确显示了的,前面的问号是数据库取来的
难道就没有人遇到这样的问题吗?
[[i] 本帖最后由 默生 于 2008-6-11 09:33 编辑 [/i]]
sdlywwb 2008-6-11 09:43
我昨天遇到这个情况已经解决了
把database.php里面的
$db['default']['char_set'] = "gb2312";
$db['default']['dbcollat'] = "gb2312_chinese_ci";
数据库中都设置成gb2312
谁有好用的验证码共享下!急需
nase 2008-6-11 15:34
[url]http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html[/url]
zhuqx1130 2008-8-24 21:30
我是插入中文数据时 数据库里显示乱码,但取出来的数据显示在页面是正常的
szlinz 2008-9-20 10:31
UTF-8对中文的支持就是不爽,经常遇到各种各样的乱码错误。
yz20sui 2008-9-20 20:37
乱码的根源 在于 数据库->动态语言->html 三个环节中 编码不一致导致
如果基于CI开发 database 配置里里面设置好 编码模式后
还要保证 html 有 对应格式的输出头
数据库编码 也必须与之一致 另外浏览器编码设置为自动
gz123 2008-9-22 11:26
楼上说得很对,数据库编码,CI设置编码,HTML编码都设置成UTF8就肯定不会有问题,你的问题应该是数据库的编码不对,好好查查数据库
sam 2008-10-23 18:53
[quote]原帖由 [i]qq123[/i] 于 2008-10-23 16:38 发表 [url=http://codeigniter.org.cn/forums/redirect.php?goto=findpost&pid=6291&ptid=669][img]http://codeigniter.org.cn/forums/images/common/back.gif[/img][/url]
全设成UTF8了,还那样,一堆问号。 [/quote]
ci中要设置,数据库要设置,html要设置,并且编码一致,还不行,那就是汉字的问题
gz123 2008-10-23 22:52
遇到过这个问题,如果你CI设置,前台VIEW的页面都设置是UTF-8还不行,就是数据库的问题,你的数据库的字符集也需要是UTF-8