[英]MySQL JSON Encoded string corrupts after INSERT..SELECT
I'm encoding array of Image URLS into json string and store them in database. 我将图像URL的数组编码为json字符串并将它们存储在数据库中。 (utf8_general_ci).
(utf8_general_ci)。
When I insert data into table and retrive it, json_decode() is capable of decoding it. 当我将数据插入表并检索它时,json_decode()能够解码它。
However, when I copy data from one table to another (INSERT INTO ... SELECT statement) data after retrieving from database cannot be decoded anymore. 但是,当我从一个表复制数据到另一个表(INSERT INTO ... SELECT语句)时,从数据库中检索后的数据不再被解码。
Instead, i get corrupted json ENCoded string. 相反,我得到了损坏的json ENCoded字符串。 Even empty array
[]
cannot be properly decoded. 即使是空数组
[]
也无法正确解码。
It converts from http://pl.tinypic.com/r/fwoiol/8 into http://pl.tinypic.com/r/bgea05/8 它从http://pl.tinypic.com/r/fwoiol/8转换为http://pl.tinypic.com/r/bgea05/8
(had to make images since those squares cannot be copied as text). (必须制作图像,因为这些正方形不能作为文本复制)。
Edit, After checking a bit more i tried to bin2hex() both strings from database. 编辑,检查了一下后,我试图从数据库bin2hex()两个字符串。
Both seem to be exactly same. 两者似乎完全相同。
However, one decodes and one does not. 但是,一个解码,一个解码。 The
该
5b22687474703a5c2f5c2f7777772e
changes into
0022687474703a5c2f5c2f7777772e
So, json_decode only changes 5b into 00 in string. 因此,json_decode仅在字符串中将5b更改为00。
It's like It's losing encoding somewhere? 这就像它在某处丢失编码?
Edit 2 编辑2
static public function jsonDecodeFieldsArray($entries, $fields = array('features','images')){
foreach($entries as $key => $entry){
$entries[$key] = self::jsonDecodeFields($entry, $fields);
}
return $entries;
}
static public function jsonDecodeFields($entry, $fields = array('features','images')){
foreach($fields as $field){
if(isset($entry[$field])){
$entry[$field] = json_decode((string) $entry[$field], true);
}
}
return $entry;
}
I'm using code above, to decode keys of array specified by $fields. 我正在使用上面的代码来解码$ fields指定的数组的键。 However, it not only decodes wrongfully.
但是,它不仅错误地解码。 But also affects keys that are not listed in $fields.
但也会影响$ fields中未列出的键。 Corrupting their encodings.
腐蚀他们的编码。
More to add. 更多要补充。 If I dont use those functions and use only json_decode on fields
json_decode($array[0][images], true)
it works fine. 如果我不使用这些函数并且在字段
json_decode($array[0][images], true)
上仅使用json_decode json_decode($array[0][images], true)
它可以正常工作。
To Clarify that I found answer/solution I write this Answer 为了澄清我找到了答案/解决方案,我写了这个答案
The reason behoind this error was not SQL error and data was proper. 原因是这个错误不是SQL错误,数据是正确的。 I had an example array of:
我有一个示例数组:
$many_entries = array(
array(
'features' = > 'json_encoded_string'
'images' = > 'json_encoded_string'
),
array(
'features' = > 'json_encoded_string'
'images' = > 'json_encoded_string'
)
);
// And
$one_entry = array(
'features' = > 'json_encoded_string'
'images' = > 'json_encoded_string'
);
Now I had 2 functions. 现在我有2个功能。 One to Parse
$many_entries
( jsonDecodeFieldsArray
) array and one to Parse $one_entry
array structure ( jsonDecodeFields
). 一个解析
$many_entries
( jsonDecodeFieldsArray
)数组,一个解析$one_entry
数组结构( jsonDecodeFields
)。
The problem was I used jsonDecodeFieldsArray
on $one_entry
which made jsonDecodeFields
iterate on strings. 问题是我在
$one_entry
jsonDecodeFieldsArray
上使用了jsonDecodeFieldsArray
,它使jsonDecodeFields
迭代字符串。
It is odd that the character encoding is changing through the transmission. 字符编码通过传输而改变是奇怪的。 I would say check your charset(s) in PHP but you said the issue is only occurring in a table => table SQL transfer.
我会说用PHP检查你的字符集,但是你说这个问题只发生在table => table SQL transfer中。 I would still check the charset of the column / table.
我仍然会检查列/表的字符集。
You can fix the issue by running a str_replace() upon decoding. 您可以通过在解码时运行str_replace()来解决此问题。 For example:
例如:
$DB_ARRAY = $DB_QUERY->fetch_array();
$CORRECT_ENCODING = json_decode(str_replace('0x93', '[', $DB_ARRAY['urlstring']), true);
You would, of course, need to know what the wrongly encoded character is. 当然,您需要知道错误编码的字符是什么。 Or its ASCII code equivalent.
或者它的ASCII码等价。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.