查詢資料庫的資料亂碼 求解決

來源:互聯網
上載者:User
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 庫名 編碼一致是不會亂碼的

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.