web前端開發 - PHP輸出json到web無法正常顯示中文

來源:互聯網
上載者:User
PHP從mysql擷取資料後,錄入數組並轉為json。
最後輸出的時候,中文顯示為"?"。
這個問題是出在伺服器還是php?
在資料庫裡看,中文是能正常顯示的。
PHP代碼:

    $result = mysql_query("select * from hpsubject");    while ($row = mysql_fetch_array($result)) {        $arr = json_encode(array('hps_userid'=>$row['hps_userid'],                            $row['hps_userid2'], $row['hps_subid'], $row['hps_type'],            $row['hps_addr'], $row['hps_distance'], $row['hps_title'], $row['hps_feel'], $row['hps_dowhat'],            $row['hps_text'], $row['hps_img'], $row['hps_like'], $row['hps_repost'], $row['hps_comment'],            $row['hps_date'], $row['hps_time'], $row['hps_liveline'], $row['hps_deadline']));    echo $arr;    }

輸出結果:
{"hps_userid":"1","0":"1","1":"1","2":"????","3":"??","4":"50","5":"??","6":"??","7":"????","8":"??","9":"??","10":"100","11":"100","12":"100","13":"2015-09-21","14":"154436","15":"2015-09-21 15:44:36","16":"2015-09-21 15:44:36"}

回複內容:

PHP從mysql擷取資料後,錄入數組並轉為json。
最後輸出的時候,中文顯示為"?"。
這個問題是出在伺服器還是php?
在資料庫裡看,中文是能正常顯示的。
PHP代碼:

    $result = mysql_query("select * from hpsubject");    while ($row = mysql_fetch_array($result)) {        $arr = json_encode(array('hps_userid'=>$row['hps_userid'],                            $row['hps_userid2'], $row['hps_subid'], $row['hps_type'],            $row['hps_addr'], $row['hps_distance'], $row['hps_title'], $row['hps_feel'], $row['hps_dowhat'],            $row['hps_text'], $row['hps_img'], $row['hps_like'], $row['hps_repost'], $row['hps_comment'],            $row['hps_date'], $row['hps_time'], $row['hps_liveline'], $row['hps_deadline']));    echo $arr;    }

輸出結果:
{"hps_userid":"1","0":"1","1":"1","2":"????","3":"??","4":"50","5":"??","6":"??","7":"????","8":"??","9":"??","10":"100","11":"100","12":"100","13":"2015-09-21","14":"154436","15":"2015-09-21 15:44:36","16":"2015-09-21 15:44:36"}

if (function_exists('mysql_set_charset') === false) {   mysql_query("SET NAMES 'utf8'", $con);}else{   mysql_set_charset('utf8', $con);}

$con 對應 mysql_connect 函數的傳回值, 上面的 utf8 改為你資料庫的編碼(上面的代碼放在你執行mysql_query("select * from hpsubject")之前.

然後在你的php檔案頭部增加一行header('Content-Type: application/json; charset=utf-8'); 其中這裡的 utf-8 也改為你資料庫中所使用的編碼.

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.