【问题标题】:MySQL PHP JSON null textMySQL PHP JSON 空文本
【发布时间】:2023-03-23 10:07:01
【问题描述】:

我发现在数据库中的某些搜索结果存在问题。当某些字段有额外的字符(如“ü”)时,该字段返回为 null,因此在搜索中显示为 null。我的代码是这样的: php脚本

 $q=mysql_query("SELECT * FROM PRODFAR WHERE ARTI LIKE '%".$_REQUEST['search']."%'");
    while($e=mysql_fetch_assoc($q))
            $output[]=$e;

    print(json_encode($output));

    mysql_close(); 

JSON PARSER 构造函数:

public class JsonParser {
    static InputStream is = null;
    static JSONObject json_data = null;
    static String result = "";

    // constructor
    public JsonParser() {
    }

    public  JSONArray getJSONFromUrl(ArrayList<NameValuePair> nameValuePairs, String url) {

        //http post this will keep the same way as it was (it's important to do not forget to add Internet access to androidmanifest.xml
        InputStream is = null;
        String result ="";
        JSONArray jArray = null;
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        }
        catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }

        //convert response that we receive from the php file into a String()
        try{


            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        // try parse the string to a Json object
        try {
            //json_data = new JSONObject(result);
            jArray = new JSONArray(result);


        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return Json String
        return jArray;

    }    
}

有什么办法可以解决这个问题吗?

更新:只要我不能用 JSON 传递它,因为它只接受 UTF-8 字符。我认为一种可能的解决方案是通过 PHP 将文本转换为 UTF-8 编码存档,另一种是使用支持其他编码的 JSON 的替代方案。所以我想尝试第一个。因此,如果有人知道使用 PHP 将文本编码转换为 UTF-8 的好算法将有所帮助。也欢迎其他提示或提示找到解决方案的可能方向请评论这篇文章欢迎任何想法

已解决 我解决了它编码为 UTF-8 的问题,它将我的字符(如“ü”)更改为类似 u\000f 的字符,但是当它显示在屏幕上时,java 编辑器将其显示为 iso-8859-1,如 Ü。修改后的 PHP 代码在查询后有以下几行:

$q=mysql_query("SELECT * FROM PRODFAR WHERE ARTI LIKE '%$search1%'");
        while($e=mysql_fetch_assoc($q)){
              $e['ARTI'] = utf8_encode ( $e['ARTI'] );
              $e['DESC'] = utf8_encode ( $e['DESC'] );
              $e['PRESENT'] = utf8_encode ( $e['PRESENT'] );   
                $output[]=$e;
        }
        print(json_encode($output)); 

【问题讨论】:

    标签: java php mysql json utf-8


    【解决方案1】:

    JSON 仅支持 UTF8。所以尝试使用 utf8_encode() / utf8_decode() 进行转换。

    【讨论】:

    • 你知道 MySQL 上是否有一个选项可以使用 UTF-8 不区分大小写对数据进行编码。那也兼容JSON?
    • 我想让我的字符串更小,而不是为 UTF-8 盲目地转义所有内容。有没有办法可以用它们的 UTF8 等价物/最接近的替换物替换非 UTF8 字符?另外 utf8_enode 将如何影响我的 MySQL WHERE 语句,我也必须对我的条件进行编码?
    【解决方案2】:

    乍一看,我猜这是编码问题。确保您的表格是 UTF-8 并且您的视图是相同的。

    另外:请不要将$_REQUEST 数据直接放入您的SQL 中。那会回来困扰你。

    【讨论】:

    • 我已经在 MySQL 上将编码更改为 utf-8 bin,但现在我遇到了问题,因为查询不同且 "a" 为 "A",所以我的结果较少MySQL 上不区分大小写字母的 utf-8 编码?
    • @PedroTeran,只使用 utf-8,没有 bin 部分
    • 谢谢,我使用了 UTG-8_unicode,只要 phpmyadmin 不允许我只选择 UTF-8,它也与大小写无关,但是当我通过 android 执行搜索时,我仍然得到特殊字符为空
    猜你喜欢
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    相关资源
    最近更新 更多