求一个方法
php编码 js解码.
1.PHP页面编码中文,存入数据库.
2.通过数据库取出数据,封装成JSON.返回到页面,通过JS解码显示中文.
大牛有什么好方法吗?
PHP5.2版本的.

解决方案 »

  1.   

    通常是在一个循环中读取查询结果的,于是可以有
    while($row = mysql_assoc($rs)) {
      $t = array();
      foreach($row as $k=>$v) $t[] = "\"$k\":\"$v\"";
      $res = '{' . join('},{', $t) . '}';
    }
    $json = '[' . join('],[', $res) .']';
      

  2.   

    我的代码是这样取值的
    for($count=0; $row = $result->fetch_object(); $count++)
    {
    $resultArray[$count]['ID'] = $row->CLIENTID;
    $resultArray[$count]['money'] = $row->PAYNUM;
    $resultArray[$count]['Title'] = $row->PROJECTID;
    $resultArray[$count]['Mobile'] = $row->MOBILENUM;
    $resultArray[$count]['SMS_Content'] = $row->SMS;
    $resultArray[$count]['Date'] = $row->CREATETIME;
    }PHP不是有自动封装JSON的方法吗?json_encode($resultArray).就是这个啊.
    我不懂你的这个方法目的是什么?
      

  3.   


     我的代码是这样取值的
    for($count=0; $row = $result->fetch_object(); $count++)
    {
    $resultArray[$count]['ID'] = $row->CLIENTID;
    $resultArray[$count]['money'] = $row->PAYNUM;
    $resultArray[$count]['Title'] = $row->PROJECTID;
    $resultArray[$count]['Mobile'] = $row->MOBILENUM;
    $resultArray[$count]['SMS_Content'] = $row->SMS;
    $resultArray[$count]['Date'] = $row->CREATETIME;
    }PHP不是有自动封装JSON的方法吗?json_encode($resultArray).就是这个啊.
    我不懂你的这个方法目的是什么?
      

  4.   

    既然你的php版本已经支持 json_encode 了,那你还用问什么?
    既然你那样问,当然认为是你的环境不支持 json_encode 函数了
      

  5.   

    不是的,是有中文,JSON无法很好的使用.到了前台页面,就直接是null,或者是乱码
      

  6.   

    json_encode 只对 utf-8 编码有效,你要么转成 utf-8 的,要么就有我给的方法
      

  7.   

    谢谢,已经搞定了.
    我的中文数据发送流程是  JS - php -mysql ,取出是  mysql- php - json - js -html 页面.
    现在我的处理方法是,
    在数据JS 向 PHP 传送前,   
    $html=iconv("GB2312","UTF-8//IGNORE",$html);
    PHP-mysql 前
    $html= urldecode($html);然后,取出来的时候按正常的流程就行了.不需要做什么处理.