用的Andr​​oid PHP:保存UTF-8字符串到MySQL [英] Android with php: Saving utf-8 string to MySQL

查看:118
本文介绍了用的Andr​​oid PHP:保存UTF-8字符串到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我问的已经问过很多次的问题,但我觉得我需要对我的问题作出澄清。

I know I am asking a question that has been asked many times before, but I feel I need clarification on my problem.

我有一个Android应用程序发送一个JSON EN codeD字符串的PHP脚本。下面code,然后将数据保存为一个完整的JSON字符串。 (也有其他功能正常保存数据到多个领域,而不是仅仅为一个JSON字符串)

I have an Android app that sends a JSON encoded string to a PHP script. The following code then saves the data as a complete JSON string. (There are also other functions to properly save the data to multiple fields, not just as one JSON string)

$json_complete = $_POST['questionnaire'];
$q = json_decode($json_complete, true);

//save complete json string to database
$query_upload = "INSERT INTO questions_json (q_id, json) VALUES('".mysql_real_escape_string($q[qID])."', '$json_complete') ON DUPLICATE KEY UPDATE json = VALUES(json)";
$result = mysql_query($query_upload) or errorReport("Error in query: $query_upload. ".mysql_error());
if (!$result)
    errorReport($result);

问题是,一些文本包含在JSON字符串是从Android中的strings.xml是连接在UTF-8 codeD服用。正如strings.xml档案℃的例子;项目>能\\'吨那里< /项目> 另存为Canu0027t那里MySQL中。同时批发和放大器;零售。另存为批发零售u0026

The problem is that some of the text included in the JSON string is taken from strings.xml in Android that is encoded in utf-8. As an example in the strings.xml file <item>Can\'t get there</item> is saved as "Canu0027t get there" in MySQL. Also "Wholesale & Retail" is saved as "Wholesale u0026 Retail".

我也有 json_de code()在PHP中失败,对我,因为非UTF-8恩在JSON字符串codeD字符。得到了以下错误:畸形的UTF-8字符,可能是不正确的连接codeD

I also had json_decode() in php fail on me because of non utf-8 encoded characters in the JSON string. Got the following error: Malformed UTF-8 characters, possibly incorrectly encoded.

我该如何处理所有这些场景?任何帮助,请。

How can I handle all these scenarios? Any help please.

推荐答案

您还必须 mysql_real_escape_string()你的 $ json_complete 在您的查询,否则你会失去原来的 / uXXXX 编码。

You also have to mysql_real_escape_string() your $json_complete in your query, otherwise you'd loose the original /uXXXX encodings.

$json_complete = $_POST['questionnaire'];
$q = json_decode($json_complete, true);

//save complete json string to database
$query_upload = "INSERT INTO questions_json (q_id, json) VALUES('".mysql_real_escape_string($q[qID])."', '".mysql_real_escape_string($json_complete)."') ON DUPLICATE KEY UPDATE json = VALUES(json)";
$result = mysql_query($query_upload) or errorReport("Error in query: $query_upload. ".mysql_error());
if (!$result)
    errorReport($result);

这篇关于用的Andr​​oid PHP:保存UTF-8字符串到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆