Heim  >  Artikel  >  Backend-Entwicklung  >  查询数据库的数据乱码 求解决

查询数据库的数据乱码 求解决

WBOY
WBOYOriginal
2016-06-23 14:14:28891Durchsuche

1.php和html 都是utf-8
2.convention.php  里面的'DEFAULT_CHARSET'       => 'utf-8', 
3.DbMysql.class.php  改成这样也不对   $result[] = auto_charset($row, gb2312, utf8)
// 自动转换字符集 支持数组转换
function auto_charset($fContents,$from,$to){
    $from   =  strtoupper($from)=='UTF8'? 'utf-8':$from;
    $to       =  strtoupper($to)=='UTF8'? 'utf-8':$to;
    if( strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents)) ){
        //如果编码相同或者非字符串标量则不转换
        return $fContents;
    }
    if(is_string($fContents) ) {
        if(function_exists('mb_convert_encoding')){
            return mb_convert_encoding ($fContents, $to, $from);
        }elseif(function_exists('iconv')){
            return iconv($from,$to,$fContents);
        }else{
            return $fContents;
        }
    }
    elseif(is_array($fContents)){
        foreach ( $fContents as $key => $val ) {
            $_key =     auto_charset($key,$from,$to);
            $fContents[$_key] = auto_charset($val,$from,$to);
            if($key != $_key ) {
                unset($fContents[$key]);
            }
        }
        return $fContents;
    }
    else{
        return $fContents;
    }
}


解决不了了  新手求助中-----


回复讨论(解决方案)

如果只是查询数据库,则只要设置 set names utf8 就可以取出utf-8的数据了。当然了。你的表必须为utf8编码才行。

php和html 都是utf-8 的
那么在连接数据库后执行sql指令  set names utf8 即可
当然这是指数据库是 mysql 且设计的字段是支持宽字符的情况



php和html 都是utf-8 的
那么在连接数据库后执行sql指令  set names utf8 即可
当然这是指数据库是 mysql 且设计的字段是支持宽字符的情况
set names utf8  是在哪儿啊??是thinkphph 文件夹的哪个文件呢?

'DEFAULT_CHARSET'       => 'utf-8', // 默认输出编码
'DB_CHARSET'            => 'utf8',      // 数据库编码默认采用utf8

thinkphph 不需要你 set names utf8
他会自己完成

找到了根源 就是这两句啊 擦
   // $job=M("Form");
   // $list=$job->where()->find();
   $job=M("Form");
   $list=$job->select();
其他啥也没改
新手勿喷

mysql_query('set names utf-8');

出现乱码,就一定有一处字符码不同,可能你建库的时候编码就不一样,你可以查看库的编码,show create datebase 库名 编码一致是不会乱码的

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn