跪求大神thinkphp 微信 不能查询中文, 弄了2天了,快崩溃了......$text是英文正常,是中文的话就错误. Mysql编码已改成utf8_unicode_ci, thinkphp的config.php也加了'DB_CHARSET'=>'utf8', $condition['pname'] = array('like','%'.$text.'%');
$condition['pkeyword'] = array('like','%'.$text.'%');
$condition['_logic'] = 'OR';
  $data = M('ProductInfo')->where($condition)->order('prank')->limit(9)
         ->field('snid as url,pname as title,pdesp_short as description,pimg1 as picurl,pprice_retail as pprice_retail')
         ->select();
         $reply = [];
         $url="/link/chk-wechat.php?openid=%s&snid=%d";
         foreach ($data as $k => $v) {
             $reply[] = array(
              'Url' => sprintf($url,$this->openid,$v['url']),
             'Title'=>$v['title'],
             'Description'=>$v['description'],
             'PicUrl'=>$v['picurl']
            );
         }
         if($data){
     $this->news($reply)->reply();
}else{

$this->text("额,小魔的数据库里没有您要的产品. 请联系客服详洽!".$text."S:")->reply();
}

解决方案 »

  1.   

    你是查不出来 还是查出来微信不能输出?
    我看你的sql没什么问题 估计出在微信端   我以前post中文的时候也出现过类似的问题
    想要兼容中文的话 你可以试试urlencode()一下中文字符串 给你贴2个函数 虽然是转json的不过大致方法差不多public static function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
        {
            static $recursive_counter = 0;
            if (++$recursive_counter > 1000) {
                die('possible deep recursion attack');
            }
            foreach ($array as $key => $value) {
                if (is_array($value)) {
                    self::arrayRecursive($array[$key], $function, $apply_to_keys_also);
                } else {
                    $array[$key] = $function($value);
                }
         
                if ($apply_to_keys_also && is_string($key)) {
                    $new_key = $function($key);
                    if ($new_key != $key) {
                        $array[$new_key] = $array[$key];
                        unset($array[$key]);
                    }
                }
            }
            $recursive_counter--;
        }
         
    /*
     *************************************************************
     *
     *    将数组转换为JSON字符串(兼容中文)
     *    @param  array   $array      要转换的数组
     *    @return string      转换得到的json字符串
     *    @access public
     *
     ************************************************************
    */
    public static function toString($array)
    {
        self::arrayRecursive($array, 'urlencode', true);
        $json = json_encode($array);
        return urldecode($json);
    }
      

  2.   

    文件的编码是否也已设置为utf8,